Java findviewbyid在对话框中返回null

Java findviewbyid在对话框中返回null,java,android,Java,Android,我有一个自定义对话框,当我尝试获取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.

我有一个自定义对话框,当我尝试获取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;
}
试试这个:

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

您必须知道在哪个视图中查找id。否则,它将尝试从由
setContentView
膨胀的xml布局中查找视图中的id(通常在
onCreate
中声明)

我遇到了相同的问题,它在以下代码段中出现:

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
返回空值,但随后我使用了
dialog.findviewbyid(R.id.etBankName)
,它成功了。

在我的例子中: 首先我必须打电话给警察

dialog.show()

只有在它之后,我才能使用

findviewbyd(R.id.myID)


如果我没有调用show(),那么findViewByID将返回空值。

我面临类似的问题。在我的例子中,我有一个自定义布局的对话框,在这个布局中有一个单选按钮。为了解决这个问题,我使用了以下代码:

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

在我的例子中,我出现这个错误是因为我重新声明了一个初始化的变量

在主要活动中,我有:

EditText cityName;
在onCreate中:

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

刚刚删除文本,一帆风顺

现有答案中没有一个对我有效,因此我开始尝试不同的生命周期挂钩,对我有效的是
onViewCreated
,从语义上来说,这似乎也是一个不错的选择。

在我的例子中,我所做的是传递一个不同片段中的视图ID,在这种情况下,编译器给了我相同的错误。 因此,请尝试检查ID use pass是否位于连接到java文件的同一xml中。
我希望这会有所帮助这是我在这个社区给出的第一个解决方案。

这与
AlertDialog.Builder.create()
似乎暗示的完全相反,而且也是完全正确的。事实上,即使不取消对null的引用,只要调用
findviewbyd
就会导致在最终调用
show()
时引发不同的异常。谢谢,太奇怪了。。。顺便说一下,这取决于设备。在调用
Dialog.show()
之前,我使用了
Dialog.findViewById()
,它很有效。