java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pdog.testdemo/com.example.pdog.testdemo.viewpagerindicator.ViewPagerIndicatorActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class android.support.design.widget.TabLayout
项目中需要用到 tablayout 和 viewpager的组合
用tablayout一直都挺方便的,但是这次怎么也集成不了
xml文件类似如下
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_view_pager_indicator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.pdog.testdemo.viewpagerindicator.ViewPagerIndicatorActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
java代码类似如下
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
ViewPager pager = (ViewPager)findViewById(R.id.pager);
pager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return 5;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return object == view;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
TextView textView = new TextView(container.getContext());
textView.setText("sadgsg" + position);
container.addView(textView);
return textView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public CharSequence getPageTitle(int position) {
String s = "position" + position;
return s;
}
});
tabLayout.setupWithViewPager(pager);
在集成的过程中一直报布局文件 inflate
错误,报错如下
android.view.InflateException:Binary XML file line #11: Binary XML file line #11: Error inflating class android.support.design.widget.TabLayout
尝试修改Theme 但是也没有用
修改如下 ,我的项目使用的AppTheme 主题
<style name="AppBaseTheme" parent="android:Theme.Light">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowNoTitle">true</item>
</style>
最后通过对比可以运行的TestDemo 一步一步傻瓜式排除法
排除Activity 和Fragment的区别
排除AppCompact主题的问题
排除Context的区别
最后修改如下
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowNoTitle">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
就这么简单,简直无语,完全没有明白这个继承为什么没有起到效果???求解
3月28日 晨,更新
原因是
<style name="AppTheme" parent="AppBaseTheme">
继承的appBaseTheme在不同版本下有不同的parent,而我项目14api以上继承的parent 没有设置主题颜色(只是设置了默认)。。。所以一直崩溃
以后要长记性啊。。