3

My app is using the new AndroidX framework, and when setting up a com.google.android.material.navigation.NavigationView (in my activity_main.xml file), I got this error:

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)

My code is here: https://github.com/GauthamRajesh/BetterBarterSystem

Thanks for helping me solve this problem!

PS: I have looked at the possible duplicate question, and the answer for that involves the android.support library. However, since I am not using the support or design libraries in my code, the solution doesn't apply to me. The android.support version of NavigationView and the Android material version of NavigationView are different.

I tried using this link to help as well, but I couldn't find the answer: https://material.io/develop/android/components/navigation-view/

  • 1
    You forgot to add dependencies for `NavigationView` `implementation 'com.android.support:design:27.1.1'` – AskNilesh Sep 19 '18 at 04:04
  • @Nilesh He's already added that library `implementation 'com.google.android.material:material:1.0.0-rc02'` – Bek Sep 19 '18 at 04:37
  • @Bek its same issue Check the exception `Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0` – AskNilesh Sep 19 '18 at 04:43
  • its a duplicate of https://stackoverflow.com/questions/30709419/error-inflating-class-android-support-design-widget-navigationview – AskNilesh Sep 19 '18 at 04:43
  • @NileshRathod the answer says that the support and appcompat libraries should be the same. In my case, the material library and the androidx library are the same version, so the solution doesn't apply. –  Sep 19 '18 at 05:02
  • @GauthamRajesh the problem is not of lib the pro is here Check the exception `Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0` – AskNilesh Sep 19 '18 at 05:03
  • Somehow your theme is causing the problem. Use `Theme.AppCompat` instead of `Theme.MaterialComponents` and change style accordingly. – Bek Sep 19 '18 at 06:04
  • Android studio reporting wrong crash. I tested and it's working. – Bek Sep 19 '18 at 06:10
  • @Nilesh the problem was related to Theme and android studio was reporting wrong crash. – Bek Sep 19 '18 at 06:13
  • @Bek I changed my `NewMaterialTheme` to inherit from `Theme.AppCompat` and I still get the same error. –  Sep 19 '18 at 13:36
  • can you post your style and manifest xml. – Bek Sep 19 '18 at 13:54
  • or update project on the github – Bek Sep 19 '18 at 13:59
  • @Bek I updated the project on github –  Sep 19 '18 at 22:35

2 Answers2

3

Try delete all large image (background, icon, ...) in nav_header_main.xml file!

1

Change your style as below

<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>

Change your manifest to

 <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>
Bek
  • 7,790
  • 4
  • 18
  • 31
  • I got the same error, but this time for inflating the `MaterialButton`: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gaucow.betterbartersystem/com.gaucow.betterbartersystem.MainActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class com.google.android.material.button.MaterialButton –  Sep 20 '18 at 04:53
  • which version of `android studio` are you using? – Bek Sep 20 '18 at 05:46
  • I uploaded project to github https://github.com/droidapp/BetterBarterSystem. Clone it. If it doesn't work then, there's a problem with your IDE. Now even if I change style to previews version it's working correctly. – Bek Sep 20 '18 at 07:27
  • Happy to help you. – Bek Sep 22 '18 at 16:19