Andrew
Andrew

Reputation: 1067

Android appcompt library 23.1.1: TabLayout.Tab.setCustomView() NullPointerException

Since upgrading from 23.1.0 to 23.1.1 of the appcompat library, calling setCustomView() on a TabLayout.Tab throws a NullPointerException.

eg

TabLayout.Tab tab = mTabLayout.newTab();
tab.setCustomView(R.layout.tab_photo_indicator);
mTabLayout.addTab(tab);

Throws a NullPointerException on the second line. The exception points to TabLayout.java:1019 inside the appcompat library, the inflater = line below:

public Tab setCustomView(int resId) {
    final TabView tabView = mParent.getTabView(mPosition);
    final LayoutInflater inflater = LayoutInflater.from(tabView.getContext());
    return setCustomView(inflater.inflate(resId, tabView, false);
}

Downgrading back to 23.1.0 makes it work again, but 23.1.1 fixes a different issue I experience in that version.

Is there something wrong in what I'm doing or is this an issue in the support library?

Upvotes: 3

Views: 1236

Answers (2)

tiny sunlight
tiny sunlight

Reputation: 6251

Plz update com.android.support:appcompat-v7 to 23.1.1.

It's because that 'com.android.support:design:23.1.1 and com.android.support:appcompat-v7:23.1.1 adjust package structure.You should use the same version.

Upvotes: -1

Andrew
Andrew

Reputation: 1067

Adding the tab to the layout before setting the custom view avoids the crash. eg:

TabLayout.Tab tab = mTabLayout.newTab();
mTabLayout.addTab(tab);
tab.setCustomView(R.layout.tab_photo_indicator);

Unfortunately the layout doesn't display exactly as before but I was able to modify the layout to suit.

This issue also precludes creating and adding the tab as a one liner. eg:

mTabLayout.addTab(mTabLayout.newTab().setCustomView(r.layout.tab_photo_indicator));

Upvotes: 4

Related Questions