错误:在androidx中膨胀类com.google.android.material.navigation.navigationview

qzlgjiam  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(455)

我的应用程序高于api级别26或更高,但已停止工作低于api级别26。请帮帮我。。
为什么xml在膨胀类时返回错误,并在较低的api级别停止工作?
我尝试了这些方法,但没有成功:
如果您使用的是android studio中的默认导航抽屉模板代码,请检查您在com.google.android.material.navigation.navigationview(可能在activity\u main.xml中)中使用的可绘制和颜色资源的代码。检查可绘制文件是否在res/drawable文件夹中,而不是res/drawable-v21中。检查你是否使用过android:backgroundtint()或android:src or 在导航视图中类似。因为它们在androidapi级别21以下不工作,所以使用app:backgroundtint or app:srccompat instead.
...

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bozorgan/com.example.bozorgan.MainActivity}: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class com.google.android.material.navigation.NavigationView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
        at android.app.ActivityThread.access$1100(ActivityThread.java:223)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7223)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
     Caused by: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class com.google.android.material.navigation.NavigationView
        at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
        at com.example.bozorgan.MainActivity.onCreate(MainActivity.java:63)
        at android.app.Activity.performCreate(Activity.java:6877)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:223) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:7223) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
     Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class com.google.android.material.navigation.NavigationView
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    android:id="@+id/navigation_drawer">

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

    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/navigation_view"
        android:layoutDirection="rtl"
        app:headerLayout="@layout/navigation_header"
        android:layout_gravity="end"
        app:menu="@menu/menu" />

</androidx.drawerlayout.widget.DrawerLayout>
ncecgwcz

ncecgwcz1#

请确保将drawerlayout库添加到build.gradle(:app)文件中,以便可以使用drawerlayout。如果没有,打开build.gradle(:app)文件并添加:implementation“androidx.drawerlayout:drawerlayout:1.1.1”

nfs0ujit

nfs0ujit2#

在androidx中,navigationview需要这些依赖项,还可以查看文档以获取更多信息

// Java language implementation
  implementation "androidx.navigation:navigation-fragment:2.3.2"
  implementation "androidx.navigation:navigation-ui:2.3.2"

  // Kotlin
  implementation "androidx.navigation:navigation-fragment-ktx:2.3.2"
  implementation "androidx.navigation:navigation-ui-ktx:2.3.2"

相关问题