Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
694 views
in Technique[技术] by (71.8m points)

android - EditText inside AlertDialog always null

I've search over the threads but so far I have not found what I'm looking for. I created a custom Alert Dialog that show up and I can do almost anything with it. It custom dialog is made of 3 TextViews and 3 EditText but whenever I need to get the EditText I get a null component.

From my xml file

 <TextView android:layout_alignParentTop="true" 
    android:id="@+id/txtAccName" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Name"
    android:textSize="23dp"
    >

</TextView>
<EditText android:id="@+id/txtEditName" 
    android:layout_below="@+id/txtAccName" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:maxLength="20"
    >

I'm trying to get the EditText field with:

    EditText txtAccName = (EditText) findViewById(R.id.txtEditName);
    Log.d("#############################", "txtAccName="+txtAccName.getText().toString());

But when doing this, the first line works well, the second home causes a crash and the control is null. This is the Overwrite method that I use to create the custom AlertDialog that I need.

    @Override
protected Dialog onCreateDialog(int id) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View layout = inflater.inflate(R.layout.accountdialog, (ViewGroup) findViewById(R.id.accDialog));

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setNegativeButton(android.R.string.cancel,new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {

        }
        });
    builder.setPositiveButton(android.R.string.ok,
            new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                EditText txtAccName = (EditText) findViewById(R.id.txtEditName);
                //EditText txtAccCur = (EditText) findViewById(R.id.txtEditCur);
                //EditText txtAccCountry = (EditText) findViewById(R.id.txtEditCountry);
                //DBConn db = new DBConn(Accounts.this.getApplicationContext(), "msaverdb", null, 0);
                Log.d("#############################", "CALLING db.insertAccount");
                Log.d("#############################", "txtAccName="+txtAccName.getText().toString());
                //db.insertAccount(txtAccName.getText().toString(), txtAccCountry.getText().toString(), 
                //      txtAccCur.getText().toString());
                Accounts.this.removeDialog(ACC_DIALOG);
            }
            });
    builder.setNegativeButton(android.R.string.cancel,
            new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                Accounts.this.removeDialog(ACC_DIALOG);
            }
            });
    builder.setView(layout);
    AlertDialog ad = builder.create();
    return ad;
}

Any help is most appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Take note of how all of your method calls are being resolved within your anonymous inner classes.

findViewById is a method that exists on views and on your activity. The version of this method on your activity searches for a view within the activity window's view hierarchy. The version on views searches that view instance and all attached children.

Your call on the problematic line of code:

EditText txtAccName = (EditText) findViewById(R.id.txtEditName);

is resolving to Activity#findViewById. But your dialog's layout is not attached to your activity window, it's attached to the dialog. You can find the correct view reference in several ways but the simplest in your case is probably to search from the root of the layout that you inflated:

EditText txtAccName = (EditText) layout.findViewById(R.id.txtEditName);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...