android – findViewById()返回null – 尝试一切

前端之家收集整理的这篇文章主要介绍了android – findViewById()返回null – 尝试一切前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我放弃了…甚至我的教授也不知道为什么会发生这种情况.

问题是:无论我在哪里或如何调用此布局的组件,它们总是返回null.

LAYOUT: search_layout. 2 EdiText and 2 TextView children.

RUNTIME: setView(inflater.inflate(R.layout.search_layout,null)) // works fine,it displays the layout inside the dialog,yet,children are always returned as null by findViewById(R.id.some_search_layout_children)

例外:解析XML的异常,因为资源未找到,因此为空.

TRIED:清理我的项目大约1024次,尝试为我的对话框实现另一个类,同样的结果,称为findViewById作为我的主要活动的成员,在方法initSearch()内部和对我的对话框的OnClickListener的匿名实现结果.打破孩子们的独立观点,并以编程方式将他们称为:

TextView text = (TextView) findResourceById(R.id.new_independant_textview); // same result

我想我试过一切

这是我的代码

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); // returns null,ALWAYS
 // no matter HOW,WHERE,global,local final,etc,it just returns null.

以下是我的自定义布局的XML:

<?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()将在Activity的布局中搜索视图,而不是对话框的视图.您需要在您设置为对话框布局的特定视图上调用findViewById().

尝试这个

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

注意我如何使视图膨胀并将其存储在View命名的dialogView中.然后,要找到您的EditText命名为tagField,我使用dialogView.findViewById(R.id.tagField);

猜你在找的Android相关文章