findViewById() returns null for Views in a Dialog

随声附和 提交于 2019-11-26 22:58:50

Calling findViewById() will search for views within your Activity's layout and not your dialog's view. You need to call findViewById() on the specific View that you set as your dialog's layout.

Try this

private void initSearch() {
    AlertDialog.Builder searchDialog = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    searchDialog.setTitle("Search Photos");
    searchDialog.setMessage("Specify tag and value...");
    // R.layout.search_dialog is my custom layour, it displays fine, it works. 
    View dialogView = inflater.inflate(R.layout.search_dialog, null);
    searchDialog.setView(dialogView);
    EditText tagText = (EdiText) dialogView.findViewById(R.id.tagField); 
    searchDialog.setPositiveButton( ... ) ...
    AlertDialog myAlert = searchDialog.create(); //returns an AlertDialog from a Builder.
    myAlert.show();
}

Notice how I'm inflating the view and storing it in a View named dialogView. Then, to find your EditText named tagField, I'm using dialogView.findViewById(R.id.tagField);

The TextView with id text123 has to be declared inside the Layout you set with setContentView

Your problem is you are trying to do .show() on a AlertDialog Builder, not the AlertDialog itself.

Try the following code:

public class Xyz extends Activity {
public void onCreate(...) { // some listener will trigger initSearch() }

private void initSearch() {
    AlertDialog.Builder searchDialog = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    searchDialog.setTitle("Search Photos");
    searchDialog.setMessage("Specify tag and value...");
    // R.layout.search_dialog is my custom layour, it displays fine, it works. 
    searchDialog.setView(inflater.inflate(R.layout.search_dialog, null));
    EditText tagText = (EdiText) findViewById(R.id.tagField); // WILL RETURN NULL
    searchDialog.setPositiveButton( ... ) ...
    AlertDialog myAlert = searchDialog.create(); //returns an AlertDialog from a Builder.
    myAlert.show();
}

I had the exact same problem while programming a multilingual app. Eventually I found out that I forgot to update labels in some layout's activity [xml] files.

I had 3 of them per activity:

<activity_name>.xml
<activity_name>.xml(land)
<activity_name>.xml(iw)

I updated only the first and forgot to update the other as follows:

All three had the a single TextView with the id of:

    <TextView
    android:id="@+id/loading_textbox"
    .../>

Then I've changed the name of the id of that text view - only in the first file - to:

    <TextView
    android:id="@+id/status_textbox"
    .../>

And, of-course, in the Activity's Java code (that uses all three...):

    TextView tv = findViewByID(R.id.status_textbox);

This worked for the regular (English <activity_name>.xml) version.

But when I went the the IW language (Hebrew <activity_name>xml(iw)) tv got null value and I even got some crushes.

When I changed the text view id of the other files to "@+id/status_textbox" everything worked like a charm...

So, simply make sure all your IDs are updated and accounted for in all your layouts and languages.

This solved my problem anyway..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!