findViewById is returning null for me on an ImageView widget. There is no error and nothing in logcat that indicates what is going on. The id's match and other image views are being set properly. Java and xml are linked by the class tag in xml pointing to the class defined in java which is a descendant of RelativeLayout.
I tried changing the name of R.id.more_icon1 and that didn't work. 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.
What's wierd is that the other ImageView's work just fine.
Anyone seen this before or have any ideas?
Java Code: Class is a descendant of 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:
<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"
<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.
Java file:
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.
Once I removed the android:visibility="gone", the findViewById resolved correctly. All I had to do was to set the visibility in the code afterwards.
textViewText.setVisibility(TextView.GONE);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With