简体   繁体   English

为什么 findViewById(...) 返回 null?

[英]Why does findViewById(...) return null?

findViewById is returning null for me on an ImageView widget. findViewById 在 ImageView 小部件上为我返回 null。 There is no error and nothing in logcat that indicates what is going on. logcat 中没有错误,也没有任何内容表明发生了什么。 The id's match and other image views are being set properly. id 的匹配和其他图像视图正在正确设置。 Java and xml are linked by the class tag in xml pointing to the class defined in java which is a descendant of RelativeLayout. Java 和 xml 由 xml 中的 class 标记链接,指向 java 中定义的类,它是 RelativeLayout 的后代。

I tried changing the name of R.id.more_icon1 and that didn't work.我尝试更改 R.id.more_icon1 的名称,但没有用。 Tried cleaning and that didn't work.尝试清洁,但没有奏效。 Used debugger to see that it really does just move on past and when it returns mMoreIcon == null.使用调试器来查看它确实只是继续过去,并且当它返回 mMoreIcon == null 时。

What's wierd is that the other ImageView's work just fine.奇怪的是,另一个 ImageView 的工作正常。

Anyone seen this before or have any ideas?任何人以前见过这个或有任何想法?

Java Code: Class is a descendant of RelativeLayout Java 代码:Class 是 RelativeLayout 的后代

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    mText1 = (TextView) findViewById(R.id.text1);
    mText2 = (TextView) findViewById(R.id.text2);
    mIcon1 = (ImageView) findViewById(R.id.icon1);
    mIcon2 = (ImageView) findViewById(R.id.icon2);
    // mMoreIcon is the one that gets set as null. mIcon1 and mIcon2 work just fine.
    mMoreIcon = (ImageView) findViewById(R.id.more_icon1);

}

XML Code: XML 代码:

<ImageView android:id="@+id/icon1"
    style="@style/SuggestionIcon1"
    android:scaleType="centerInside"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
/>

<!--     This is the icon that is being returned as null -->
<ImageView android:id="@+id/more_icon1"
    style="@style/MoreIcon2"
    android:scaleType="centerInside"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:visibility="gone" />

<ImageView android:id="@+id/icon2"
    style="@style/SuggestionIcon2"
    android:scaleType="centerInside"
    android:layout_toLeftOf="@id/more_icon1"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:visibility="gone" />

Thanks谢谢

I just recently had a similar problem when using the android:visibility="gone"我最近在使用 android:visibility="gone" 时遇到了类似的问题

<RelativeLayout 
    android:id="@+id/relativeLayoutMessage"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/linearLayoutMenu" android:layout_above="@+id/layoutButtons">
        <ImageView android:id="@+id/imageView" 
            android:layout_width="fill_parent" 
            android:contentDescription="@string/image_attachment" 
            android:src="@drawable/icon" 
            android:scaleType="fitCenter" 
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true" /> 
        <TextView
            android:id="@+id/textViewTitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center" 
            android:textSize="20dip"
            android:layout_alignParentTop="true"/>
        <TextView
            android:id="@+id/textViewText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:visibility="gone" 
            android:gravity="center"
            android:layout_below="@+id/textViewTitle"/> 
</RelativeLayout>   

With the ImageView above the TextViews, the application would crash at the following line.使用 TextView 上方的 ImageView,应用程序将在下一行崩溃。

Java file: Java文件:

    setContentView(R.layout.display_image);

    imageView = (ImageView) findViewById(R.id.imageView);

If I moved the ImageView below the TextViews, the application would run but the TextViews were covered by the ImageView.如果我将 ImageView 移动到 TextViews 下方,应用程序将运行,但 TextViews 被 ImageView 覆盖。

Once I removed the android:visibility="gone", the findViewById resolved correctly.一旦我删除了 android:visibility="gone",findViewById 就会正确解析。 All I had to do was to set the visibility in the code afterwards.我所要做的就是在代码中设置可见性。

textViewText.setVisibility(TextView.GONE);

I had a similar issue and my problem was that findviewbyid only find child views.我有一个类似的问题,我的问题是 findviewbyid 只能找到子视图。 If the view is a sibling you'd have to call it from the parent.如果视图是兄弟视图,则必须从父视图调用它。 The activity should be able to find it.活动应该能够找到它。

I had this problem as well.我也有这个问题。 This was my code:这是我的代码:

RelativeLayout ml = (RelativeLayout) findViewById(R.id.layout);
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_the_next);

After changing it to this it worked:将其更改为此后,它起作用了:

RelativeLayout ml;
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_the_next);
ml = (RelativeLayout) findViewById(R.id.layout);

It seems that if you instantiate your layout before you setContentView() it returns null.似乎如果在 setContentView() 之前实例化布局,它会返回 null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM