【问题标题】:Binary XML Error inflating class com.google.android.material.navigation.NavigationView膨胀类 com.google.android.material.navigation.NavigationView 的二进制 XML 错误
【发布时间】:2020-02-06 10:45:52
【问题描述】:

我已在 StackOverflow 上进行了广泛的检查,这是一个常见问题,但我未能使用任何解决方案来解决我的问题。我将我的应用程序迁移到 AndoridX,然后我的应用程序在 NavigationView 上迁移后开始崩溃。但是,如果我注释掉NavigationView 的代码,则该应用程序运行正常。请查看我所有的 xml 代码和错误日志。 我已经在同一主题上检查了this 相关问题,但我的崩溃没有从答案中得到解决:

布局代码:

layout_base.xml

    <?xml version="1.0" encoding="utf-8"?>

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

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <View
                android:id="@+id/anchor_view"
                android:layout_width="20sp"
                android:layout_height="5sp"
                android:layout_alignParentRight="true"></View>
        </RelativeLayout>

        <androidx.drawerlayout.widget.DrawerLayout
            android:id="@+id/drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/child"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <include layout="@layout/layout_tool_bar">

                </include>

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

                </RelativeLayout>
            </LinearLayout>

            <com.google.android.material.navigation.NavigationView
                android:id="@+id/navigation"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="start">


                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ListView
                        android:id="@+id/lst_menu_items"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_above="@+id/ll_menu_options"
                        android:dividerHeight="0.0sp"
                        android:scrollbars="none" />

                    <LinearLayout
                        android:id="@+id/ll_menu_options"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_above="@+id/iv_powered_by"
                        android:layout_marginLeft="15dp"
                        android:layout_marginRight="20dp"
                        android:layout_marginBottom="15dp"
                        android:orientation="horizontal"
                        android:paddingTop="5dp"
                        android:visibility="gone">

                        <LinearLayout
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:gravity="center_horizontal"
                            android:orientation="vertical"
                            android:visibility="invisible">

                            <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                 />

                        </LinearLayout>

                        <LinearLayout
                            android:id="@+id/ll_facebook"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:gravity="center_horizontal"
                            android:orientation="vertical">

                            <ImageView
                                android:id="@+id/iv_facebook"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                />
                        </LinearLayout>

                        <LinearLayout
                            android:id="@+id/ll_twitter"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:gravity="center_horizontal"
                            android:orientation="vertical"
                            android:visibility="visible">

                            <ImageView
                                android:id="@+id/iv_twitter"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                             />
                        </LinearLayout>

                        <LinearLayout
                            android:id="@+id/ll_instagram_page"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:gravity="center_horizontal"
                            android:orientation="vertical"
                            android:visibility="visible">

                            <ImageView
                                android:id="@+id/iv_in"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                 />


                        </LinearLayout>

                        <LinearLayout
                            android:id="@+id/ll_gplus"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:gravity="center_horizontal"
                            android:orientation="vertical"
                            android:visibility="invisible">

                            <ImageView
                                android:id="@+id/iv_gplus"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                 />

                        </LinearLayout>
                    </LinearLayout>

                    <ImageView
                        android:id="@+id/iv_powered_by"
                        android:layout_width="match_parent"
                        android:layout_height="9dp"
                        android:layout_alignParentBottom="true"
                        android:layout_marginBottom="18dp"
                        android:visibility="gone" />

                </RelativeLayout>
            </com.google.android.material.navigation.NavigationView>

        </androidx.drawerlayout.widget.DrawerLayout>


        <include layout="@layout/layout_fab"></include>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</RelativeLayout>

应用程序在此行崩溃:setContentView(R.layout.layout_base_activity);

我也在使用Theme.AppCompat.Light.NoActionBar 主题(查看comment below

错误日志:

android.view.InflateException: Binary XML file line #48: Binary XML file line #48: Error inflating class com.google.android.material.navigation.NavigationView
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
            at android.app.ActivityThread.-wrap11(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
         Caused by: android.view.InflateException: Binary XML file line #48: Binary XML file line #48: Error inflating class com.google.android.material.navigation.NavigationView
         Caused by: android.view.InflateException: Binary XML file line #48: 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:334)
            at android.view.LayoutInflater.createView(LayoutInflater.java:647)
            at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.createCustomViewInternal(CalligraphyLayoutInflater.java:211)
            at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.access$000(CalligraphyLayoutInflater.java:20)
            at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater$PrivateWrapperFactory2.onCreateView(CalligraphyLayoutInflater.java:302)
            at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:186)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
            at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
            at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
            at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
            at com.pentaloop.playerxtreme.presentation.activities.FileManagerActivity.onCreate(FileManagerActivity.java:155)
            at android.app.Activity.performCreate(Activity.java:7009)
            at android.app.Activity.performCreate(Activity.java:7000)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
            at android.app.ActivityThread.-wrap11(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)

build.gradle

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.github.lecho:hellocharts-library:1.5.8@aar'
implementation 'com.google.android.gms:play-services-analytics:15.0.2'
implementation 'com.google.android.gms:play-services-ads:15.0.1'
implementation 'com.makeramen:roundedimageview:2.3.0'
implementation 'org.apache.commons:commons-lang3:3.7'
implementation 'uk.co.chrisjenx:calligraphy:2.2.0'
implementation 'com.loopj.android:android-async-http:1.4.9'
implementation 'jp.wasabeef:blurry:3.0.0'
implementation 'de.hdodenhof:circleimageview:3.0.1'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.github.satyan:sugar:1.5'
implementation 'info.hoang8f:android-segmented:1.0.6'
implementation 'jcifs:jcifs:1.3.17'
implementation 'com.android.support:multidex:1.0.1'
implementation 'com.squareup.okhttp3:okhttp:3.9.1'
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.jpardogo.googleprogressbar:library:1.2.0'
implementation 'com.github.pwittchen:reactivewifi-rx2:0.3.0'
implementation 'com.github.pwittchen:reactivenetwork-rx2:3.0.6'
implementation 'com.karumi:dexter:5.0.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.ext.kotlinx_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.ext.kotlinx_version"

//Android X

implementation "androidx.preference:preference:1.0.0"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'com.google.android.material:material:1.1.0-alpha07'

}

PS:我查看了可能的重复问题,答案涉及android.support 库。但是,由于我没有在代码中使用支持或设计库,因此该解决方案不适用于我。

【问题讨论】:

  • 您使用的是哪个应用主题?
  • 分享你的应用级别build.gradle文件。
  • @Muhammad Awais 我分享了 build.gradle 文件,请检查
  • @MuhammadTufail,你的 targetSdkVersion 是否高于 28?
  • @GabrieleMariotti 我正在使用 Theme.AppCompat.Light.NoActionBar 主题

标签: android android-layout navigation-drawer androidx material-components-android


【解决方案1】:

DrawerLayout 中,您使用了android:drawingCacheQuality="low"。当设置为低质量时,绘图缓存使用较低的颜色深度,从而失去渲染渐变的精度,因此使用更少的内存。但是android:drawingCacheQuality已经被弃用了,见官方文档here。以编程方式改用 Canvas 和 Pixel Api。请参阅@mostafa-monowar 的this answer 了解实现。

【讨论】:

  • 如果我删除这一行 android:drawingCacheQuality="low" 那么应用程序也会崩溃。
  • 我没有告诉你删除,仔细阅读,实现 Pixel Api 来实现同样的效果。此外,要解决当前问题,请检查其他 2 个 XML 布局或发布您已包含在此布局代码中的问题中的代码,即 layout_tool_barlayout_fab
  • 当我删除 com.google.android.material.navigation.NavigationView 应用程序工作正常时,我在问题中提到
  • 问题出在 NavigationView 中,因为当我删除此应用时运行
  • @SaadAAkashi 使用 androidx 在单独的项目中创建了一个抽屉,它工作正常,但是当我在我的项目中添加相同的代码时,应用程序崩溃并出现相同的错误
【解决方案2】:

您正在使用Theme.AppCompat.Light.NoActionBar 主题。
getting started 文档中所述,在您的应用中,您必须使用Material Components Theme

类似:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.*">
    <!-- ... -->
</style>

使用其中一种:

Theme.MaterialComponents
Theme.MaterialComponents.NoActionBar
Theme.MaterialComponents.Light
Theme.MaterialComponents.Light.NoActionBar
Theme.MaterialComponents.Light.DarkActionBar
Theme.MaterialComponents.DayNight
Theme.MaterialComponents.DayNight.NoActionBar
Theme.MaterialComponents.DayNight.DarkActionBar

如果您无法将主题更改为从 Material Components 主题继承,您可以从 Material Components Bridge theme 继承。

【讨论】:

  • 感谢您的回复。我更改了我的应用程序的主题,但应用程序再次崩溃我认为当我删除导航视图时崩溃出现在 NavigationView 我不知道为什么应用程序运行良好而没有任何崩溃或错误
  • 我已经使用 androidx 在单独的项目中创建了一个抽屉,它工作正常,但是当我在我的项目中添加相同的代码时,应用程序崩溃并出现相同的错误
猜你喜欢
  • 2020-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多