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

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

自从 appcompat 库从 23.1.0 升级到 23.1.1 后,在 TabLayout.Tab 上调用 setCustomView() 会抛出 NullPointerException

例如

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

在第二行抛出一个 NullPointerException。异常指向 appcompat 库内的 TabLayout.java:1019,下面的 inflater = 行:

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);
}

降级回 23.1.0 使其再次运行,但 23.1.1 修复了我在该版本中遇到的另一个问题。

我的操作有问题吗?或者这是支持库中的问题吗?

在设置自定义视图之前将选项卡添加到布局可以避免崩溃。例如:

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

不幸的是,布局与以前显示的不完全一样,但我能够修改布局以适应。

这个问题还排除了创建和添加选项卡作为一个衬里。例如:

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

请更新 com.android.support:appcompat-v7 到 23.1.1.

因为'com.android.support:design:23.1.1和com.android.support:appcompat-v7:23.1.1调整包structure.You应该使用相同的版本