NullPointerException when use findViewById() in AlertDialog.Builder

陌路散爱 提交于 2019-12-01 08:00:44

问题


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?


回答1:


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();



回答2:


Search for the DatePicker using the dialog parameter:

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



回答3:


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;

}




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/13026141/nullpointerexception-when-use-findviewbyid-in-alertdialog-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!