Question

[Solved] Why does findViewById(…) return null?

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

Solution #1:

I had a similar issue and my problem was that findviewbyid only find child views. If the view is a sibling you’d have to call it from the parent. The activity should be able to find it.

Respondent: danca

Solution #2:

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);
Respondent: Phidius

Solution #3:

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.

Respondent: dodo

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy