[Tab Items] Tab Items does not work with View Binding, throws NPE
See original GitHub issueDescription: Filing this in response to ja…@google.com comment on https://issuetracker.google.com/issues/145687658
a…@tandu.it
I have a view which contains some TabItem with an ID. When I try to generate my binding with .inflate() the app throws an NPE on those views. This happens on both Activity and Fragment.
ja…@google.com :
My working theory is material library is doing something horrific like intercepting TabLayout.addView and not actually putting the views into the hierarchy synchronously.
ja…@google.com :
Indeed, that is the problem.
2019-12-09 15:57:32.859 6425-6425/? I/System.out: FrameLayout ID:16908290 2019-12-09 15:57:32.859 6425-6425/? I/System.out: LinearLayout ID:2131230917 2019-12-09 15:57:32.859 6425-6425/? I/System.out: TabLayout ID:-1 2019-12-09 15:57:32.859 6425-6425/? I/System.out: SlidingTabIndicator ID:-1 2019-12-09 15:57:32.859 6425-6425/? I/System.out: TabView ID:-1 2019-12-09 15:57:32.859 6425-6425/? I/System.out: ImageView ID:-1 2019-12-09 15:57:32.859 6425-6425/? I/System.out: TextView ID:-1 2019-12-09 15:57:32.859 6425-6425/? I/System.out: TabView ID:-1 2019-12-09 15:57:32.859 6425-6425/? I/System.out: ImageView ID:-1 2019-12-09 15:57:32.859 6425-6425/? I/System.out: TextView ID:-1
Please file a bug on the Google Material library that it should consider retaining the IDs if one was supplied.
I’ll repurpose this issue for honoring our
tools:viewBindingIgnore
attribute on arbitrary XML nodes to at least work around the issue.
Expected behavior: Tab Items working with View Binding without throwing NPEs
Source code: Required source files could be found at https://issuetracker.google.com/issues/145687658
Material Library version:
Android Material dependency: com.google.android.material:material:1.1.0-beta01
Issue Analytics
- State:
- Created 3 years ago
- Reactions:13
- Comments:7 (1 by maintainers)
Top GitHub Comments
Still happening, I found a workaround, try this:
Go to your XML and search the ID that is generating the issue and delete its android:id
Use the TabLayout API in your class to access the TabItems.
For example, this is my XML:
<com.google.android.material.tabs.TabItem android:id=“@+id/tbItem” <!-- Delete this line -> android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:layout_gravity=“bottom” android:backgroundTintMode=“screen” android:text=“@string/mosaic” />
Go to your class: And use this:
val tabView: TabLayout = binding.tbType //This is my tab layout tabView.getTabAt(0)!!.text = “Example 1”
When will this bug be fixed? Still happening with 1.3.0-rc01