FindViewById (int) returns null for custom views, but not native Android

Situation: I have some custom components in my layout. I have a general layout layout that I load in the Activity onCreate() base class, and then load the content layouts in my implementations with the help of a blower, setting the root in the content column of the main layout.

Problem:. When I get a view link to retrieve user input, Activity.findViewById() returns null . Perhaps this is a hint that CheckBox and Button in the layout DO NOT return null ; I get a valid widget link.

Things I tried: I know that I load and inflate the xml layout correctly, because everything appears, and the standard views in the same layout can be found by ID. I can interact with my views and put content in them and that’s all, but I can’t get a link to them in my code.

I tried to clean the project several times. R.id is fresh and updated.

I checked the Console and the Error Log, and there were no UI / XML error messages there.

I tried to get a handle to the root layout of the content loaded for this action, and call View.findViewById() to get my links, and also returns null . If I look at the layout in the debugger, I can expand it and find it in mChildren .

Perhaps another key:

 public VideoChooser(Context pCtxt, AttributeSet pAttrs) { super(pCtxt, pAttrs); Log.d("VideoChooser", "Expected ID: " + R.id.vchShareVids + " | actual: " + getId()); } 

will lead to the following conclusion:

DEBUG/VideoChooser(10372): Expected ID: 2131296271 | actual: 268435456

The identifier assigned to the view does not match the identifier in R.id ! Why is this so? I know that it loads the android:id attribute, otherwise it will be -1 ( View.NO_ID ).

General layout layout:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:foo="http://schemas.android.com/apk/res/com.foo" android:id="@+id/common_frame" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- top banner --> <LinearLayout android:id="@+id/frame_header" android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_marginBottom="16dp"> <ImageView android:src="@drawable/banner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" /> </LinearLayout> <!-- content column --> <LinearLayout android:id="@+id/frame_content" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="32dp" android:layout_marginRight="32dp" /> </LinearLayout> 

Content Layout:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:foo="http://schemas.android.com/apk/res/com.foo" android:id="@+id/content_panel" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.foo.view.VideoChooser android:id="@+id/vchShareVids" foo:prompt_text="@string/prompt_share_vid" foo:prompt_size="16dp" foo:preview_height="80dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="12dp" android:hapticFeedbackEnabled="true" /> <com.foo.view.ContactChooser android:id="@+id/cchRecipients" foo:prompt_text="@string/prompt_share_email" foo:prompt_size="16dp" foo:preview_lines="3" foo:dialog_title="Pretend you are picking contacts" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="12dp" android:hapticFeedbackEnabled="true" /> <com.foo.view.TextChooser android:id="@+id/tchDescription" foo:prompt_text="@string/prompt_share_description" foo:prompt_size="16dp" foo:preview_lines="1" foo:dialog_title="@string/title_msg_chooser_dlg" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="12dp" android:hapticFeedbackEnabled="true" /> <CheckBox android:id="@+id/chkReshare" android:text="@string/prompt_reshare" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:hapticFeedbackEnabled="true" /> <Button android:id="@+id/btnSend" android:text="@string/btn_send" android:layout_width="@dimen/btn_width" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:hapticFeedbackEnabled="true" /> </LinearLayout> 

The main activity class onCreate ():

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.common_frame); } 

Implementation of onCreate () activity:

 @Override protected void onCreate(Bundle pState) { super.onCreate(pState); load_content_view(R.layout.content_layout); ViewGroup tLayout = (ViewGroup)findViewById(R.id.content_panel); // These all return null mCchVideo = (ContentChooser)tLayout.findViewById(R.id.vchShareVids); mCchContact = (ContentChooser)tLayout.findViewById(R.id.cchRecipients); mCchDescription = (ContentChooser)tLayout.findViewById(R.id.tchDescription); // These return valid references mChkReshare = (CheckBox)findViewById(R.id.chkReshare); mBtnSend = (Button)findViewById(R.id.btnSend); // ... } protected void load_content_view(int pResId) { LinearLayout tColumn = (LinearLayout)findViewById(R.id.frame_content); getLayoutInflater().inflate(pResId, tColumn); } 
+4
source share
3 answers

Yes ... I'm an idiot. In the view, of course, the correct identifier was set, and then one of my init methods came back and held it together.

Facepalm

+1
source

I had a similar problem and the solution for mine was to make sure the custom widget constructor calls

 super(context, attrs); 

My constructor did not pass attrs super, and so the view id was corrupted, and findviewbyId returned null.

+12
source

It is very difficult to find a problem without real sources. I created a sample project based on your posts and it works completely. I believe that there is a very simple mistake, and you will find it. If you want, you can try

+1
source

All Articles