对于对话框中的视图,findViewById()返回null [英] findViewById() returns null for Views in a Dialog

查看:101
本文介绍了对于对话框中的视图,findViewById()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是,无论我在何处或以何种方式调用此布局的组件,它们始终返回null.

The problem is, no matter where or how I call for this layout's components, they always return null.

setView(inflater.inflate(R.layout.search_layout, null))

这很好.它显示了Dialog内部的布局,但是,findViewById(R.id.some_search_layout_children)始终将子代返回为null.

This works fine. It displays the layout inside the Dialog, yet, the children are always returned as null by findViewById(R.id.some_search_layout_children).

我尝试多次清理项目,尝试为我的Dialog实现另一个类,称为findViewById()作为我的主Activity的成员,在initSearch()方法内部和匿名实现中DialogOnClickListener,但所有结果相同.我还尝试过将孩子分成独立的View并以编程方式调用他们:

I've tried cleaning my project multiple times, tried to implement another class for my Dialog, called findViewById() as a member of my main Activity, inside the initSearch() method, and inside an anonymous implementation of OnClickListener for the Dialog, but all with the same result. I've also tried breaking the children out into independent Views and programmatically calling them:

TextView text = (TextView) findResourceById(R.id.new_independant_textview);

但是,同样的结果.

这是相关代码:

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( ... ) ...
        searchDialog.show();
    }

此行:

 EditText text = (EditText) findViewById(R.id.tagField);

始终调用null,无论调用方式或位置如何(全局,本地final等),它仅返回null.

always returns null, no matter how or where it's called – globally, local final, etc. – it just returns null.

这是我的自定义Dialog布局的XML:

Here is the XML of my custom Dialog layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/search_dialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/tagText" 
        android:padding="7dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:text="@string/tag" />
    <EditText 
        android:id="@+id/tagField"
        android:padding="7dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"/>
    <TextView
        android:id="@+id/valueText" 
        android:padding="7dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:text="@string/value" />
    <EditText 
        android:id="@+id/valueField"
        android:padding="7dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"/>
</LinearLayout>

这是我的R.java文件:

public static final class id {
    public static final int action_settings=0x7f0a0011;
    public static final int add_album=0x7f0a0001;
    public static final int add_photo=0x7f0a000d;
    public static final int albums_list=0x7f0a0003;
    public static final int delete_album=0x7f0a000b;
    public static final int exit_finder=0x7f0a000f;
    public static final int new_directory=0x7f0a000e;
    public static final int open_album=0x7f0a000a;
    public static final int photos_grid=0x7f0a0000;
    public static final int rename_album=0x7f0a000c;
    public static final int search_dialog=0x7f0a0004;
    public static final int search_icon=0x7f0a0002;
    public static final int splash_rutgers=0x7f0a0009;
    public static final int tagField=0x7f0a0006; // problematic
    public static final int tagText=0x7f0a0005; / problematic
    public static final int terminate_app=0x7f0a0010;
    public static final int valueField=0x7f0a0008; // problematic
    public static final int valueText=0x7f0a0007; // problematic
}

推荐答案

调用findViewById()会在您的活动布局中搜索视图,而在不是对话框视图中进行搜索.您需要在设置为对话框布局的特定View上调用findViewById().

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.

尝试一下

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

注意如何扩大视图并将其存储在名为dialogViewView中.然后,要找到名为tagFieldEditText,我正在使用dialogView.findViewById(R.id.tagField);

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

这篇关于对于对话框中的视图,findViewById()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆