原因:android.view.InflateException:二进制XML文件第16行:膨胀类com.google.android.material.navigation.NavigationView时出错

时间:2018-09-19 04:03:28

标签: java android material-design navigationview androidx

我的应用程序正在使用新的AndroidX框架,并且在我的activity_main.xml文件中设置com.google.android.material.navigation.NavigationView时,出现此错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gaucow.betterbartersystem/com.gaucow.betterbartersystem.MainActivity}: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class com.google.android.material.navigation.NavigationView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
        at android.app.ActivityThread.-wrap14(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6688)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
     Caused by: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class com.google.android.material.navigation.NavigationView
     Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class com.google.android.material.navigation.NavigationView
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
        at android.view.LayoutInflater.createView(LayoutInflater.java:652)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:525)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:378)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.gaucow.betterbartersystem.MainActivity.onCreate(MainActivity.java:38)
        at android.app.Activity.performCreate(Activity.java:6912)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
        at android.app.ActivityThread.-wrap14(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6688)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
        at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:202)
        at android.content.res.Resources.getColorStateList(Resources.java:1861)
        at android.content.Context.getColorStateList(Context.java:542)
        at androidx.appcompat.content.res.AppCompatResources.getColorStateList(AppCompatResources.java:67)
        at com.google.android.material.navigation.NavigationView.createDefaultColorStateList(NavigationView.java:513)
        at com.google.android.material.navigation.NavigationView.<init>(NavigationView.java:153)
        at com.google.android.material.navigation.NavigationView.<init>(NavigationView.java:104)

我的代码在这里:https://github.com/GauthamRajesh/BetterBarterSystem

感谢您帮助我解决了这个问题!

PS:我已经研究了可能重复的问题,这个问题的答案涉及android.support库。但是,由于我没有在代码中使用支持或设计库,因此该解决方案不适用于我。 android.support的{​​{1}}版本和NavigationView的Android实质版本是不同的。

我也尝试使用此链接来提供帮助,但找不到答案:https://material.io/develop/android/components/navigation-view/

2 个答案:

答案 0 :(得分:1)

尝试删除nav_header_main.xml文件中的所有大图像(背景,图标,...)!

答案 1 :(得分:0)

如下更改样式

<style name="AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

将清单更改为

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name_real"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:ignore="GoogleAppIndexingWarning">
    <activity android:name=".SignIn" android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:theme="@style/AppTheme.NoActionBar"
        android:name=".MainActivity"
        android:label="@string/title_activity_main"/>
</application>