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

以后要长记性啊。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,460评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,973评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,970评论 6 342
  • 一年的时间,好像无限漫长,又仿佛一瞬而逝。 2015年,我从备战中考,到端坐于考场,再到踏上新的征途,听起来需要...
    唐君临阅读 302评论 0 0
  • 提到时间,脑子里冒出来的词是,过去、现在和未来。 除了从这么老土的看时间外,还可以如何看待她呢? 不要维持单一的时...
    付晓阅读 422评论 0 0