简体   繁体   English

findviewbyid 在对话框中返回 null

[英]findviewbyid returns null in a dialog

I have a custom dialog and when I try to get the value of an EditText it returns null.我有一个自定义对话框,当我尝试获取 EditText 的值时,它返回 null。

This line returns null这一行返回空

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

Here is the code in its entirety.这是完整的代码。

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

In my case: First I must call the就我而言:首先我必须调用

dialog.show(),对话框显示(),

and only after it I was able to use只有在它之后我才能使用

dialog.findviewById(R.id.myID). dialog.findviewById(R.id.myID)。

If I missed to call the show(), than I got a null back with findViewByID.如果我错过了调用 show(),那么我会通过 findViewByID 得到一个空值。

Try this:尝试这个:

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

You have to tell in which view to find the id.你必须告诉在哪个视图中找到 id。 Otherwise it will try to find the id in the view from the xml layout inflated by setContentView (usually declared in onCreate )否则,它会尝试从由setContentView膨胀的 xml 布局(通常在onCreate声明)中查找视图中的 id

I faced a similar problem.我遇到了类似的问题。 In my case, I had a dialog with custom layout and in this layout had a radioButton.就我而言,我有一个带有自定义布局的对话框,并且在这个布局中有一个单选按钮。 In order to solve that, I used the follow code:为了解决这个问题,我使用了以下代码:

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

I was having the same problem, which presented itself in the following code snippet:我遇到了同样的问题,它出现在以下代码片段中:

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 was returning null value, but then I used dialog.findviewbyid(R.id.etBankName) and it worked. etBankName返回空值,但后来我使用了dialog.findviewbyid(R.id.etBankName)并且它起作用了。

In my case I had this error because I was redeclaring an initialized variable在我的情况下,我有这个错误,因为我重新声明了一个初始化的变量

In main Activity I had:在主要活动中,我有:

EditText cityName;

And in onCreate:在 onCreate 中:

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

Just removed EditText and smooth sailing!刚刚删除了 EditText 并且一帆风顺!

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

In my case what I did was that I was passing an ID of a view which was in different fragment and in that case the compiler gave me same error.就我而言,我所做的是传递了一个视图的 ID,该 ID 位于不同的片段中,在这种情况下,编译器给了我同样的错误。 So try checking that the ID use pass is in the same xml which is connect to the java file.因此,请尝试检查 ID 使用通行证是否在连接到 java 文件的同一个 xml 中。 I hope it helps this is my first ever solution given in this community.我希望它有帮助,这是我在这个社区中给出的第一个解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM