引用Android Design包出现的错误

在用Android Studio开发的过程中,一遇到废弃、不被推荐的方法和类,我就想做点什么去掉上面的横线。然后,被一个不是问题的问题困扰了很久。

之前我们在创建固定Tabs的时候,类似于这样的功能,

使用的是ActionBar.Tab但是ActionBar deprecated。那什么是新的解决方案呢?TabLayout+ViewPager就能实现上述的功能

出现的错误:android.view.InflateException: Binary XML file line #8: Error inflating class android.support.design

首先,我明确一下,我的sdk升级到了21版本以上。support Library 也是最新的。

在Android studio添加依赖

build.gradle(Module.app)

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:support-v4:22.2.0'
    compile 'com.android.support:design:22.2.0'
}
activity_main.xml内容如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:background="@android:color/white" />

</LinearLayout>

错误信息提示:

Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class android.support.design.widget.TabLayout
        at android.view.LayoutInflater.createView(LayoutInflater.java:633)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
        at android.app.Activity.setContentView(Activity.java:2144)
        at com.example.hp.myapplication.MainActivity.onCreate(MainActivity.java:15)
        at android.app.Activity.performCreate(Activity.java:5933)
上面提示的错误是,在xml文件中的第8行,出现了Inflate错误,然后导致MainActivity.java:15行中的

setContentView(R.layout.activity_main);
错误。

解决错误的方法:

在res/values中的styles.xml中,需要修改parent的属性值,不能使用自定义的。

这里面有两个styles.xml都需要修改

修改内容,参考如下:

<resources>

    <style name="AppTheme" parent="Base.AppTheme">
        <!-- Customize your theme here. -->
    </style>
    <!-- Base application theme. -->
    <style name="Base.AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">#673AB7</item>
        <item name="colorPrimaryDark">#512DA8</item>
        <item name="colorAccent">#FF4081</item>
    </style>

</resources>
修改parent中的属性值,与上面定义的对应
<resources>
    <style name="AppTheme" parent="Base.AppTheme">
    </style>
</resources>

结果就成功解决了。

类似问题参考链接:http://stackoverflow.com/questions/30547323/error-when-using-any-android-design-support-library-elements/30557995#30557995



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值