Arizona1911
Arizona1911

Reputation: 2201

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

Upvotes: 36

Views: 33350

Answers (7)

groot_1970
groot_1970

Reputation: 1

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.

Upvotes: 0

covercash2
covercash2

Reputation: 183

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.

Upvotes: 0

Mihai
Mihai

Reputation: 26784

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!

Upvotes: 0

Leonardo Costa
Leonardo Costa

Reputation: 1014

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

Upvotes: 18

narancs
narancs

Reputation: 5294

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.

Upvotes: 50

Vishuv Sethi
Vishuv Sethi

Reputation: 39

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.

Upvotes: 4

ccheneson
ccheneson

Reputation: 49410

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)

Upvotes: 40

Related Questions