开发者

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.

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

If I missed to call the show(), than I got a null back with findViewByID.


Try this:

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

You have to tell in which view to find the id. Otherwise it will try to find the id in the view from the xml layout inflated by setContentView (usually declared in onCreate)


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.


In my case I had this error because I was redeclaring an initialized variable

In main Activity I had:

EditText cityName;

And in onCreate:

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

Just removed EditText and smooth sailing!


None of the existing answers worked for me, so I started trying different lifecycle hooks, and the one that worked for me was onViewCreated, which seems like a good choice semantically as well.


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. So try checking that the ID use pass is in the same xml which is connect to the java file. I hope it helps this is my first ever solution given in this community.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜