【问题标题】:findviewbyid returns null in a dialogfindviewbyid 在对话框中返回 null
【发布时间】:2011-07-28 09:14:18
【问题描述】:

我有一个自定义对话框,当我尝试获取 EditText 的值时,它返回 null。

这行返回null

EditText et = (EditText)findViewById(R.id.username_edit);

这是完整的代码。

protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_TEXT_ENTRY:
        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;
}

【问题讨论】:

    标签: java android


    【解决方案1】:

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

    dialog.show(),

    只有在它之后我才能使用

    dialog.findviewById(R.id.myID)。

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

    【讨论】:

    • 这与AlertDialog.Builder.create() 似乎暗示的完全相反,而且完全正确。事实上,即使您不取消对 null 的引用,当您最终调用 show() 时,只需调用 findViewById 就会引发不同的异常。谢谢。
    • 这太奇怪了……顺便说一句,取决于设备。我在调用Dialog.show() 之前使用过Dialog.findViewById() 并且它有效。
    • 我在show() 之后调用了它,但它仍然为空。
    【解决方案2】:

    试试这个:

    EditText et = (EditText)textEntryView.findViewById(R.id.username_edit);
    

    你必须告诉在哪个视图中找到 id。否则它将尝试从由setContentView(通常在onCreate中声明)膨胀的xml布局中找到视图中的id

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题。就我而言,我有一个带有自定义布局的对话框,并且在这个布局中有一个单选按钮。为了解决这个问题,我使用了以下代码:

      View dialogLayout = factory.inflate(R.layout.dialog_layout, null);
      AlertDialog.Builder dialog = new AlertDialog.Builder(context);
      dialog.setView(dialogLayout);
      
      RadioButton radiobt = (RadioButton) dialogLayout.findViewById(R.id.radioBt);
      

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题,出现在下面的代码 sn-p 中:

        final Dialog dialog = new Dialog(this);
        
        dialog.setContentView(R.layout.addbank_dialog);
        dialog.show();
        
        Button btnSaveBank = (Button)dialog.findViewById(R.id.btnSaveBank);
        
        final EditText etBankName = (EditText)dialog.findViewById(R.id.etBankName);
        
        btnSaveBank.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    String bank = etBankName.getText().toString();
                    SharedCommonData.dbOps.saveBankInDB(bank);
                }
                catch(Exception e){
                    e.printStackTrace();
                }
                Toast.makeText(SharedCommonData.context, "Bank Saved", Toast.LENGTH_SHORT);
                refreshBanks();
                dialog.dismiss();
            }
        });
        

        etBankName 返回 null 值,但后来我使用了 dialog.findviewbyid(R.id.etBankName),它成功了。

        【讨论】:

          【解决方案5】:

          在我的情况下,我遇到了这个错误,因为我正在重新声明一个初始化变量

          在我的主要活动中:

          EditText cityName;
          

          在 onCreate 中:

          EditText cityName = (EditText)findViewById(R.id.cityName);
          

          刚刚删除了EditText,一帆风顺!

          【讨论】:

            【解决方案6】:

            现有的答案都不适合我,所以我开始尝试不同的生命周期钩子,对我有用的是onViewCreated,这在语义上似乎也是一个不错的选择。

            【讨论】:

              【解决方案7】:

              就我而言,我所做的是传递了一个视图的 ID,该视图位于不同的片段中,在这种情况下,编译器给了我同样的错误。 因此,请尝试检查 ID 使用通行证是否在连接到 java 文件的同一 xml 中。 我希望它有所帮助这是我在这个社区中给出的第一个解决方案。

              【讨论】:

                猜你喜欢
                • 2013-04-18
                • 1970-01-01
                • 2011-03-16
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多