This is my code
@Override
public void onClick(View v) {
final AlertDialog.Builder adb = new AlertDialog.Builder(getApplicationContext());
adb.setView(LayoutInflater.from(getApplicationContext()).inflate(R.layout.custom, null));
adb.setPositiveButton("Add",new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
DatePicker datePicker = (DatePicker)findViewById(R.id.datePicker);
java.util.Date date = null;
Calendar cal = GregorianCalendar.getInstance();
cal.set(datePicker.getYear(),datePicker.getMonth(), datePicker.getDayOfMonth());
date = cal.getTime();
}
});
adb.show();
}
I have the NullPointerException in this line, and I think datePicker wasn't findById, because I use AlertDialog.Builder.
cal.set(datePicker.getYear(),datePicker.getMonth(), datePicker.getDayOfMonth());
I tried use adb.findViewById();
but it'is a mistake (The method findViewById(int) is undefined for the type AlertDialog.Builder).
Can you help me, please?
A project clean and rebuild fixed this error for me (on Xamarin).
Search for the
DatePicker
using thedialog
parameter:Change
this line to
...............
try this one:
I think this could be the problem,
Use the below way to inflate the view and use the view object for getting the id.
Example:
}
In my case: First I must call the
and only after it I was able to use
If I missed to call the show(), than I got a null back with findViewByID.