NavigationView报错小记

在开始之前,先记录一下今天因为使用design库中的NavigationView报错而引发的一场解决风暴。(简直想吐血…)

一、NavigationView错误(探索总结)

  1. 出了什么错误?
java.lang.RuntimeException: Unable to start activity ComponentInfo{marketcloud.com.marketcloudexample/marketcloud.com.marketcloudexample.MainActivity}: android.view.InflateException: Binary XML file line #30: Error inflating class android.support.design.widget.NavigationView

翻译过来就是:无法找到NavigationView这个类。
2. 怎么解决?
基乎硬着头发看完了stackoverflow上各位前辈的的解答。有人说是引入的库版本号有问题、有人说是不能在styles.xml中使用这个属性:

<item name="android:textColorPrimary">@android:color/white</item>

还有些莫名的改了些东西就好了,我试遍了能找到的方法,然而还是没有作用,这让我又一次陷入了迷惑。最后,我更新了sdk和buildTools的版本,并且将引入design库的降低了版本,如下:

    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.android.support:design:23.2.0'

特别需要注意的是,引入兼容包和design库时其大版本号要和当前的 compileSdkVersion一致,贴出完整的配置:

apply plugin: 'com.android.application'
apply plugin: 'android-apt'

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.3"
    defaultConfig {
        applicationId "me.funnyzhao.mangostreet"
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.android.support:design:23.2.0'
    apt 'com.jakewharton:butterknife-compiler:8.4.0'
    compile 'com.jakewharton:butterknife:8.4.0'
    testCompile 'junit:junit:4.12'
}

至此顺利解决。
3. 总结
这个问题的出现会是如下几种情况:
R1:support:appcompat的版本号与support:design的不一致、
R2:检查dependencies中的版本是否与compileSdkVersion大版本号不一致、
R3:在styles.xml中使用textColorPrimary的颜色值输入不正确(应该用系统颜色),如下:

<item name="android:textColorPrimary">@android:color/white</item>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值