Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tab Items] Tab Items does not work with View Binding, throws NPE #1162

Closed
kaushalyap opened this issue Mar 28, 2020 · 7 comments
Closed

[Tab Items] Tab Items does not work with View Binding, throws NPE #1162

kaushalyap opened this issue Mar 28, 2020 · 7 comments
Assignees

Comments

@kaushalyap
Copy link

Description:
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

@kaushalyap kaushalyap added the bug label Mar 28, 2020
@carterhudson
Copy link

Also an issue opened here https://issuetracker.google.com/issues/152606440

@paour
Copy link

paour commented Sep 15, 2020

Still happening with com.google.android.material:material:1.2.1.

@reallysunny
Copy link

same here. Noticed on com.google.android.material:material:1.2.1 Any work around?

@dzzchao
Copy link

dzzchao commented Jan 20, 2021

When will this bug be fixed?
Still happening with 1.3.0-rc01

@acugniere
Copy link

Still happening ... Nearly 1 year later. Someone has a workaround ?

@jorgealdana30
Copy link

jorgealdana30 commented Feb 11, 2021

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"

@hunterstich
Copy link
Contributor

I don't think this is something we plan on supporting since it would require a significant change in the way TabLayout manages each TabItem. com.google.material.tab.TabItem is used more as a struct that just tells TabLayout what text, icon, and customLayout a client would like for each tab. Using those parameters, TabLayout, by default, then inflates its own TabView that is what actually gets added to the TabLayout ViewGroup. If ids of TabItems are retained, data binding will generate a binding class that expects TabItems but will find TabViews and throw a ClassCastException. This issue becomes more interesting/complicated when you're passing in custom layout for tabs.

If there's a way to override the view type given to a TabItem in the generated data binding class, that might be an option—I'm not aware of this being possible but @JakeWharton might have more info.

I would suggest sticking with @Dreft12's suggestion of using binding.tabLayout.getTabAt(<index>) which returns a Tab, from which you can get/set the item's text, view, etc.

I'm going to close this for now, but feel free to re-open if there are thoughts/questions/suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants