【问题标题】:NullPointerException when use findViewById() in AlertDialog.Builder在 AlertDialog.Builder 中使用 findViewById() 时出现 NullPointerException
【发布时间】:2012-10-23 08:17:17
【问题描述】:

这是我的代码

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

我在这一行中有 NullPointerException,我认为 datePicker 不是 findById,因为我使用 AlertDialog.Builder。

cal.set(datePicker.getYear(),datePicker.getMonth(), datePicker.getDayOfMonth());

我尝试使用adb.findViewById();,但这是一个错误(对于 AlertDialog.Builder 类型,方法 findViewById(int) 未定义)。 你能帮帮我吗?

【问题讨论】:

    标签: android dialog nullpointerexception findviewbyid


    【解决方案1】:

    改变

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

    这一行到

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

    .......

    试试这个:

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

    【讨论】:

    • The method findViewById(int) is undefined for the type AlertDialog.Builder 如果我添加到演员表 ((Dialog)adb).findViewById 我有Cannot cast from AlertDialog.Builder to Dialog
    • 如果不起作用,你为什么接受它?编辑:是的,第二部分是有效的。使用膨胀的布局而不是对话框。
    • 对我来说关键是view.findViewById(R.id.datePicker1);,特别是你使用view而不是你的Activity来调用findViewById()
    • 请删除第一个例子:)
    【解决方案2】:

    使用dialog 参数搜索DatePicker

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

    【讨论】:

    • Eclipse 说Cannot cast from AlertDialog.Builder to Dialog
    • @pavelartlover 您是否使用了我的 exact 代码,以及 onClick 方法中的 dialog 参数,并且您得到了 Cannot cast...
    • 我将您的dialog 替换为我的adb。在这种情况下,也许我使用了错误类型的对话框。我想显示带有日期选择器和我的 xml 文件中的其他内容的自定义对话框,以及警报对话框中的两个按钮 - 保存和取消。
    • @pavelartlover 再次完全使用我在回答中所说的内容。
    【解决方案3】:

    我认为这可能是问题所在,

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

    使用下面的方式来膨胀视图并使用视图对象来获取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);
    

    例子:

    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;
    

    }

    【讨论】:

    • 是的,我忘了给 R.layout.xxx 充气
    【解决方案4】:

    就我而言: 首先我必须调用

    dialog.show(),

    只有在它之后我才能使用

    dialog.findviewById(R.id.myID)。

    如果我错过了调用 show(),那么我会通过 findViewByID 得到一个空值。

    【讨论】:

      【解决方案5】:

      清理和重建项目为我修复了此错误(在 Xamarin 上)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-15
        • 2021-06-10
        • 1970-01-01
        • 1970-01-01
        • 2016-11-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多