2

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?

pavelartlover
  • 202
  • 2
  • 15

5 Answers5

15

Change

DatePicker datePicker = (DatePicker)findViewById(R.id.datePicker);

this line to

DatePicker datePicker = (DatePicker)adb.findViewById(R.id.datePicker);

...............

try this one:

final AlertDialog.Builder adb = new AlertDialog.Builder(this);
final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.main1, null);
adb.setView(view);
final Dialog dialog;
adb.setPositiveButton("Add",new android.content.DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int arg1) {

        DatePicker datePicker = (DatePicker)view.findViewById(R.id.datePicker1);

        java.util.Date date = null;
        Calendar cal = GregorianCalendar.getInstance();
        cal.set(datePicker.getYear(),datePicker.getMonth(), datePicker.getDayOfMonth());
        date = cal.getTime();
    }                  
});   
dialog = adb.create();
dialog.show();
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Khawar Raza
  • 15,870
  • 24
  • 70
  • 127
2

Search for the DatePicker using the dialog parameter:

DatePicker datePicker = (DatePicker) ((Dialog) dialog).findViewById(R.id.datePicker);
user
  • 86,916
  • 18
  • 197
  • 190
  • Eclipse said `Cannot cast from AlertDialog.Builder to Dialog` – pavelartlover Oct 23 '12 at 08:26
  • @pavelartlover Did you use my **exact** code, with the `dialog` parameter from the `onClick` method and you get that `Cannot cast...`? – user Oct 23 '12 at 08:33
  • I replace your `dialog` by my `adb`. Maybe I use wrong type of dialog in this case. I want to show custom dialog with datepicker and other stuff from my xml file, and two button like in alert dialog - Save and Cancel. – pavelartlover Oct 23 '12 at 09:03
  • @pavelartlover Once again, use **exactly** what I said in my answer. – user Oct 23 '12 at 09:06
2

I think this could be the problem,

final AlertDialog.Builder adb = new AlertDialog.Builder(getApplicationContext());
adb.setView(LayoutInflater.from(getApplicationContext()).inflate(R.layout.custom,null));

Use the below way to inflate the view and use the view object for getting the id.

 LayoutInflater factory = LayoutInflater.from(this);
    final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);

DatePicker datePicker = (DatePicker)textEntryView.findViewById(R.id.datePicker1);

Example:

protected Dialog onCreateDialog() {
    LayoutInflater factory = LayoutInflater.from(this);
    final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
    return new AlertDialog.Builder(TicTacToe.this)
        //.setIconAttribute(android.R.attr.alertDialogIcon)
        .setTitle(getTitleText())
        .setView(textEntryView)
        .setPositiveButton("JOIN GAME", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                try
                {
                    EditText et = (EditText)findViewById(R.id.username_edit);
                        playerName = et.getText().toString();
                }
                catch (Exception e)
                {
                }
            }
        })
        .create();
return null;

}

sidhanshu
  • 356
  • 2
  • 9
0

In my case: First I must call the

dialog.show(),

and only after it I was able to use

dialog.findviewById(R.id.myID).

If I missed to call the show(), than I got a null back with findViewByID.

narancs
  • 5,234
  • 4
  • 41
  • 60
0

A project clean and rebuild fixed this error for me (on Xamarin).

mvandillen
  • 904
  • 10
  • 17