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
項(xiàng)目中需要用到 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);
在集成的過(guò)程中一直報(bào)布局文件 inflate
錯(cuò)誤,報(bào)錯(cuò)如下
android.view.InflateException:Binary XML file line #11: Binary XML file line #11: Error inflating class android.support.design.widget.TabLayout
嘗試修改Theme 但是也沒(méi)有用
修改如下 ,我的項(xiàng)目使用的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>
最后通過(guò)對(duì)比可以運(yùn)行的TestDemo 一步一步傻瓜式排除法
排除Activity 和Fragment的區(qū)別
排除AppCompact主題的問(wèn)題
排除Context的區(qū)別
最后修改如下
<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>
就這么簡(jiǎn)單,簡(jiǎn)直無(wú)語(yǔ),完全沒(méi)有明白這個(gè)繼承為什么沒(méi)有起到效果???求解
3月28日 晨,更新
原因是
<style name="AppTheme" parent="AppBaseTheme">
繼承的appBaseTheme在不同版本下有不同的parent,而我項(xiàng)目14api以上繼承的parent 沒(méi)有設(shè)置主題顏色(只是設(shè)置了默認(rèn))。。。所以一直崩潰
以后要長(zhǎng)記性啊。。