NullPointerException when use findViewById() in Al

2020-03-29 02:28发布

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?

5条回答
小情绪 Triste *
2楼-- · 2020-03-29 03:10

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

查看更多
▲ chillily
3楼-- · 2020-03-29 03:14

Search for the DatePicker using the dialog parameter:

DatePicker datePicker = (DatePicker) ((Dialog) dialog).findViewById(R.id.datePicker);
查看更多
不美不萌又怎样
4楼-- · 2020-03-29 03:16

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();
查看更多
成全新的幸福
5楼-- · 2020-03-29 03:25

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;

}

查看更多
Evening l夕情丶
6楼-- · 2020-03-29 03:33

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.

查看更多
登录 后发表回答