Why does findViewById (...) return null?

findViewById returns null for me in the ImageView widget. There is no error in logcat and nothing is displayed, which indicates what is happening. Correspondence of identifier and other images is correctly configured. Java and xml are linked by a class tag in xml pointing to a class defined in java, which is a descendant of RelativeLayout.

I tried changing the name R.id.more_icon1 and it did not work. Tried to clean and it didn't work. Used by the debugger to see that it really just moves through the past and when it returns mMoreIcon == null.

Well, especially since another ImageView works.

Has anyone seen this before or had 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

+4
source share
3 answers

I had a similar problem and my problem was that findviewbyid only detects child views. If the submission is a sibling, you will have to call it from the parent. This activity should be able to find it.

+6
source

I recently had a similar problem when using 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> 

Using ImageView over TextViews, the application will be split to the next line.

Java file:

  setContentView(R.layout.display_image); imageView = (ImageView) findViewById(R.id.imageView); 

If I moved ImageView to TextViews, the application would start, but TextViews were covered by ImageView.

As soon as I removed android: visibility = "gone", findViewById is correctly resolved. All I had to do was set the visibility in the code.

 textViewText.setVisibility(TextView.GONE); 
+6
source

I also had this problem. 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, 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 setting setContentView (), it returns null.

+3
source

Source: https://habr.com/ru/post/1385611/


All Articles