Design 包下的TabLayout的一个坑

  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 没有设置主题颜色(只是设置了默认)。。。所以一直崩溃

以后要长记性啊。。

转载于:https://juejin.im/post/5a31ee42f265da43195648a7

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值