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

Fue*_*ers 5 android android-widget android-layout

findViewById在ImageView小部件上为我返回null.没有错误,logcat中没有任何内容表明发生了什么.正确设置了id的匹配和其他图像视图.Java和xml由xml中的class标记链接,指向java中定义的类,它是RelativeLayout的后代.

我尝试更改R.id.more_icon1的名称,但这不起作用.尝试清洁,但没有奏效.使用调试器来查看它确实只是移动过去以及何时返回mMoreIcon == null.

什么是奇怪的是,其他ImageView的工作正常.

有人见过这个或有任何想法吗?

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

}
Run Code Online (Sandbox Code Playgroud)

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" />
Run Code Online (Sandbox Code Playgroud)

谢谢

小智 8

我最近在使用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>   
Run Code Online (Sandbox Code Playgroud)

使用TextViews上方的ImageView,应用程序将在以下行崩溃.

Java文件:

    setContentView(R.layout.display_image);

    imageView = (ImageView) findViewById(R.id.imageView);
Run Code Online (Sandbox Code Playgroud)

如果我将ImageView移动到TextViews下面,应用程序将运行,但ImageView会覆盖TextView.

一旦我删除了android:visibility ="gone",findViewById就能正确解析.我所要做的就是之后在代码中设置可见性.

textViewText.setVisibility(TextView.GONE);
Run Code Online (Sandbox Code Playgroud)


dan*_*nca 6

我有一个类似的问题,我的问题是findviewbyid只找到子视图.如果视图是兄弟姐妹,则必须从父母那里调用它.活动应该能够找到它.