findViewById()返回null - 尝试一切 [英] findViewById() returns null - tried EVERYTHING

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

问题描述

我放弃......甚至我的教授知道为什么会这样。

现在的问题是:无论在哪里,我怎么称呼此布局的组成部分,他们总是返回null

  

布局:search_layout。 2 EdiText和2个TextView的孩子。

 运行:的setView(inflater.inflate(R.layout.search_layout,NULL))//工作正常,它显示的布局的对话框内,然而,孩子们总是返回空通过findViewById(R.id.some_search_layout_children)
 

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

尝试:打扫我的项目1024左右时间,努力实现另一个类我的对话框中,相同的结果,叫做findViewById作为我的主要活动的一员,该方法initSearch(内部)和一个匿名实现OnClickListener里面我对话框,同样的结果。打破了孩子成为独立的意见和编程叫他们:

  TextView的文字=(TextView的)findResourceById(R.id.new_independant_textview); //相同的结果
 

我觉得我什么都试过了。

这是我的code:

 公共类XYZ延伸活动{
    公共无效的onCreate(...){//一些听众会触发initSearch()}

    私人无效initSearch(){
        AlertDialog.Builder searchDialog =新AlertDialog.Builder(本);
        LayoutInflater充气= this.getLayoutInflater();
        sea​​rchDialog.setTitle(搜索照片);
        sea​​rchDialog.setMessage(指定标记和价值......);
        // R.layout.search_dialog是我的自定义layour,它显示细腻,它的工作原理。
        sea​​rchDialog.setView(inflater.inflate(R.layout.search_dialog,NULL));
        EditText上标记文本=(EdiText)findViewById(R.id.tagField); //将返回NULL
        sea​​rchDialog.setPositiveButton(...)...
        sea​​rchDialog.show();
    }
 

现在,当我这样做:

 的EditText文本=(EditText上)findViewById(R.id.tagField); //返回null,总是
 //不管如何,在何处,全球性的,局部决赛等,它只是返回null。
 

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

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / search_dialog
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直>
    <的TextView
        机器人:ID =@ + ID /标记文本
        机器人:填充=7DP
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSIZE =14sp
        机器人:文本=@字符串/标签/>
    <的EditText
        机器人:ID =@ + ID / tagField
        机器人:填充=7DP
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:inputType =文本/>
    <的TextView
        机器人:ID =@ + ID / valueText
        机器人:填充=7DP
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSIZE =14sp
        机器人:文本=@字符串/值/>
    <的EditText
        机器人:ID =@ + ID / valueField
        机器人:填充=7DP
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:inputType =文本/>
< / LinearLayout中>
 

这是我的R.java文件:

 公共静态最后的类ID {
    公共静态最终诠释action_settings = 0x7f0a0011;
    公共静态最终诠释add_album = 0x7f0a0001;
    公共静态最终诠释add_photo = 0x7f0a000d;
    公共静态最终诠释albums_list = 0x7f0a0003;
    公共静态最终诠释delete_album = 0x7f0a000b;
    公共静态最终诠释exit_finder = 0x7f0a000f;
    公共静态最终诠释new_directory = 0x7f0a000e;
    公共静态最终诠释open_album = 0x7f0a000a;
    公共静态最终诠释photos_grid = 0x7f0a0000;
    公共静态最终诠释rename_album = 0x7f0a000c;
    公共静态最终诠释search_dialog = 0x7f0a0004;
    公共静态最终诠释search_icon = 0x7f0a0002;
    公共静态最终诠释splash_rutgers = 0x7f0a0009;
    公共静态最终诠释tagField = 0x7f0a0006; //问题
    公共静态最终诠释标记文本= 0x7f0a0005; /有问题
    公共静态最终诠释terminate_app = 0x7f0a0010;
    公共静态最终诠释valueField = 0x7f0a0008; //问题
    公共静态最终诠释valueText = 0x7f0a0007; //问题
}
 

感谢您的帮助,快乐的狩猎。

解决方案

调用 findViewById()将搜索的看法您的活动的布局内不会您对话的看法。你需要调用 findViewById()具体的查看您设置为你的对话框的布局。

试试这个

 私人无效initSearch(){
    AlertDialog.Builder searchDialog =新AlertDialog.Builder(本);
    LayoutInflater充气= this.getLayoutInflater();
    sea​​rchDialog.setTitle(搜索照片);
    sea​​rchDialog.setMessage(指定标记和价值......);
    // R.layout.search_dialog是我的自定义layour,它显示细腻,它的工作原理。
    查看dialogView = inflater.inflate(R.layout.search_dialog,NULL);
    sea​​rchDialog.setView(dialogView);
    EditText上标记文本=(EdiText)dialogView.findViewById(R.id.tagField);
    sea​​rchDialog.setPositiveButton(...)...
    AlertDialog myAlert = searchDialog.create(); //从生成器返回AlertDialog。
    myAlert.show();
}
 

请注意我膨胀的观点并将其存储在一个查看 dialogView 。然后,找到你的的EditText tagField ,我用 dialogView.findViewById(R .id.tagField);

I give up... not even my professor knows why this is happening.

The problem is: No matter where or how I call for this layout's components, they always return 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)

EXCEPTION: Parsing exception of the XML, as resource not found, hence null.

TRIED: Cleaned my project around 1024 times, tried to implement another class for my dialog box, same result, called findViewById as a member of my main activity, inside the method initSearch() and inside an anonymous implementation of OnClickListener for my dialog, SAME RESULT. Broke the children into independent views and programmatically called them:

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

I think I tried everything.

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

Now, when I do:

 EditText text = (EditText) findViewById(R.id.tagField); // returns null, ALWAYS
 // no matter HOW, WHERE, global, local final, etc, it just returns null.

Here is the XML of my custom 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>

This is my R.java file:

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
}

Thanks for your help, happy hunting.

解决方案

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

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

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