Android 4 Caused by java.lang.NoClassDefFoundError: android.support.design.internal.NavigationMenu

Android 4 Caused by java.lang.NoClassDefFoundError: android.support.design.internal.NavigationMenu

主要是在 Android 4 版本上崩溃。

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xx.xx/com.xx.xx.ui.activity.MainActivity}: android.view.InflateException: Binary XML file line #81: Error inflating class android.support.design.widget.NavigationView
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
   at android.app.ActivityThread.access0(ActivityThread.java:151)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
   at android.os.Handler.dispatchMessage(Handler.java:110)
   at android.os.Looper.loop(Looper.java:193)
   at android.app.ActivityThread.main(ActivityThread.java:5299)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
   at dalvik.system.NativeStart.main(NativeStart.java)
Caused by android.view.InflateException: Binary XML file line #81: Error inflating class android.support.design.widget.NavigationView
   at android.view.LayoutInflater.createView(LayoutInflater.java:620)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(Unknown Source)
   at android.support.v7.app.AppCompatActivity.setContentView(Unknown Source)
   at com.xx.xx.ui.activity.MainActivity.onCreate(Unknown Source)
   at android.app.Activity.performCreate(Activity.java:5264)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
   at android.app.ActivityThread.access0(ActivityThread.java:151)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
   at android.os.Handler.dispatchMessage(Handler.java:110)
   at android.os.Looper.loop(Looper.java:193)
   at android.app.ActivityThread.main(ActivityThread.java:5299)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
   at dalvik.system.NativeStart.main(NativeStart.java)
Caused by java.lang.reflect.InvocationTargetException
   at java.lang.reflect.Constructor.constructNative(Constructor.java)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
   at android.view.LayoutInflater.createView(LayoutInflater.java:594)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(Unknown Source)
   at android.support.v7.app.AppCompatActivity.setContentView(Unknown Source)
   at com.xx.xx.ui.activity.MainActivity.onCreate(Unknown Source)
   at android.app.Activity.performCreate(Activity.java:5264)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
   at android.app.ActivityThread.access0(ActivityThread.java:151)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
   at android.os.Handler.dispatchMessage(Handler.java:110)
   at android.os.Looper.loop(Looper.java:193)
   at android.app.ActivityThread.main(ActivityThread.java:5299)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
   at dalvik.system.NativeStart.main(NativeStart.java)
Caused by java.lang.NoClassDefFoundError: android.support.design.internal.NavigationMenu
   at android.support.design.widget.NavigationView.(Unknown Source)
   at android.support.design.widget.NavigationView.(Unknown Source)
   at java.lang.reflect.Constructor.constructNative(Constructor.java)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
   at android.view.LayoutInflater.createView(LayoutInflater.java:594)
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
   at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(Unknown Source)
   at android.support.v7.app.AppCompatActivity.setContentView(Unknown Source)
   at com.xx.xx.ui.activity.MainActivity.onCreate(Unknown Source)
   at android.app.Activity.performCreate(Activity.java:5264)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
   at android.app.ActivityThread.access0(ActivityThread.java:151)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
   at android.os.Handler.dispatchMessage(Handler.java:110)
   at android.os.Looper.loop(Looper.java:193)
   at android.app.ActivityThread.main(ActivityThread.java:5299)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
   at dalvik.system.NativeStart.main(NativeStart.java)

搜了一下,以为是proguard的问题

所以,我将这段代码添加到 'proguard-rules' 文件中。

  -keep class android.support.v4.** { *; }
  -keep interface android.support.v4.** { *; }

  -keep class android.support.design.** { *; }
  -keep interface android.support.design.** { *; }

但崩溃不断发生。 奇怪的是,其中大部分都发生在 Android 4。 'NavigationMenu' 有什么问题?这是支持库错误吗?

-- 添加了xmls

-activity_main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- The main content view -->
    <RelativeLayout
        android:id="@+id/content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_above="@+id/adView_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <LinearLayout
            android:id="@+id/adView_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical">
            <View
                android:id="@+id/divider"
                android:layout_width="match_parent"
                android:layout_height="1dp"
      android:background="@color/material_black_disabled_and_hint_text" />

            <com.google.android.gms.ads.AdView
                android:id="@+id/adView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                ads:adSize="SMART_BANNER"
                ads:adUnitId="@string/banner_ad_unit_id">
            </com.google.android.gms.ads.AdView>
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/toolbar_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/color_primary"
                android:orientation="vertical">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/toolbar_height"
                    android:background="?attr/colorPrimary"
                    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

                <android.support.design.widget.TabLayout
                    android:id="@+id/tabLayout"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/tab_height"
                    style="@style/TabLayoutCustomStyle" />
            </LinearLayout>

        <View
            android:id="@+id/toolbar_shadow"
            android:layout_below="@id/toolbar_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/header_shadow"/>
        </RelativeLayout>
    </RelativeLayout>

    <!-- The navigation drawer -->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        android:theme="@style/DrawerLayoutTheme"
        app:menu="@menu/drawer_menu"/>
</android.support.v4.widget.DrawerLayout>

-layout/drawer_header.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/iv_drawer"
      android:contentDescription="@string/app_name"
      android:layout_width="match_parent"
      android:layout_height="@dimen/drawer_iv_height"
      android:scaleType="centerCrop"
      android:background="@color/drawer_iv_background"
     android:src="@drawable/drawer_header_img" />

-menu/drawer_menu.xml

<?xml version="1.0" encoding="utf-8"?>
  <menu xmlns:android="http://schemas.android.com/apk/res/android">
      <group
          android:id="@+id/main_menu"
          android:checkableBehavior="single" >
          <item
              android:id="@+id/drawer_box"
              android:icon="@drawable/ic_box_office_black_36dp"
              android:title="@string/box_office"/>
          <item
              android:id="@+id/drawer_coming"
              android:icon="@drawable/ic__preview_black_36dp"
              android:title="@string/coming_soon"/>

          <item
              android:id="@+id/drawer_donation"
              android:icon="@drawable/ic_donation"
              android:title="@string/store"/>

          <item
              android:id="@+id/dummy"
              android:title=""
              android:enabled="false"/>
      </group>

      <group
          android:id="@+id/sub_menu"
          android:checkableBehavior="none" >

          <item
              android:id="@+id/drawer_info"
              android:title="@string/info"/>

          <item
              android:id="@+id/drawer_setting"
              android:title="@string/setting"/>
      </group>
 </menu>

-style/DrawerLayoutTheme

<style name="DrawerLayoutTheme" parent="Widget.Design.NavigationView">
    <item name="colorPrimary">@color/drawer_text_icon_color</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="android:panelBackground">@android:color/transparent</item>
    <item name="android:panelFullBackground">@android:color/transparent</item>
    <item name="android:panelColorBackground">@android:color/transparent</item>
    <item name="colorControlNormal">@android:color/white</item>
    <item name="colorControlHighlight">@color/drawer_selected_item</item>
    <item name="itemIconTint">@color/drawer_text_icon_color</item>
    <item name="android:textColorPrimary">@color/drawer_text_icon_color</item>
    <item name="android:textSize">@dimen/drawer_tv_text_size</item>
</style>

似乎有什么东西阻止了您的导航视图膨胀。可能是 api 级别的问题。看看 post。它说 "android: ..." 标签需要 api 级别 21+。

尝试从导航视图中删除样式。如果可行,请将带有 "android:.." 标签的样式项放在 values-v21 下,将其他样式项放在 values 文件夹下。