首页 > 解决方案 > mainactivity 62 er logcat

问题描述

我有以下错误:

java.lang.RuntimeException: Unable to start Activity
ComponentInfo{com.example.cn/com.example.cn.MainActivity}:
android.view.InflateException: Binary XML file line #69: Binary XML file line #69: Error inflating class androidx.support.design.widget.NavigationView

主要活动.xml:

<?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:id="@+id/drawable_layout"  
android:layout_width="match_parent"  
android:layout_height="match_parent"  
android:paddingLeft="10dp"  
android:paddingBottom="5dp"  
tools:context="com.example.cn.MainActivity"  
android:paddingStart="10dp"  
tools:ignore="RtlSymmetry">  
<RelativeLayout  
android:layout_width="match_parent"  
android:layout_height="match_parent">  
<include  
android:id="@+id/main_page_toolbar"  
layout="@layout/app_bar_layout">  
</include>  
<FrameLayout  
android:id="@+id/main_container"  
android:layout_width="match_parent"  
android:layout_height="673dp"  
android:layout_alignTop="@+id/main_page_toolbar"  
android:layout_alignParentStart="true"  
android:layout_alignParentLeft="true"  
android:layout_marginStart="0dp"  
android:layout_marginLeft="0dp"  
android:layout_marginTop="58dp">  
<androidx.recyclerview.widget.RecyclerView  
android:id="@+id/all_user_post_list"  
android:layout_width="match_parent"  
android:layout_height="match_parent" />  
</FrameLayout>  
<ImageButton  
android:id="@+id/add_new_post_button"  
android:layout_width="165dp"  
android:layout_height="57dp"  
android:layout_alignParentStart="true"  
android:layout_alignParentLeft="true"  
android:layout_alignParentTop="true"  
android:layout_alignParentEnd="true"  
android:layout_alignParentRight="true"  
android:layout_marginStart="246dp"  
android:layout_marginLeft="246dp"  
android:layout_marginTop="0dp"   
android:layout_marginEnd="0dp"  
android:layout_marginRight="0dp"  
android:background="@color/colorPrimaryDark"  
android:scaleType="centerCrop"  
android:src="@drawable/add_post_high"  
android:contentDescription="@string/app_name" />  
</RelativeLayout>  
<androidx.support.design.widget.NavigationView  
android:id="@+id/navigation_view"  
android:layout_width="wrap_content"  
android:layout_height="match_parent"  
android:layout_gravity="start"  
android:layout_marginBottom="3dp"  
app:menu="@menu/navigation_menu" />  
</androidx.drawerlayout.widget.DrawerLayout>  

标签: javaandroid

解决方案


Afaik,AndroidX 中没有androidx.support.design.widget.NavigationView类。但您可以com.google.android.material.navigation.NavigationView用作替代品。

因此,更改以下内容:

<androidx.support.design.widget.NavigationView  
    android:id="@+id/navigation_view"  
    android:layout_width="wrap_content"  
    android:layout_height="match_parent"  
    android:layout_gravity="start"  
    android:layout_marginBottom="3dp"  
    app:menu="@menu/navigation_menu" />

至:

<com.google.android.material.navigation.NavigationView 
    android:id="@+id/navigation_view"  
    android:layout_width="wrap_content"  
    android:layout_height="match_parent"  
    android:layout_gravity="start"  
    android:layout_marginBottom="3dp"  
    app:menu="@menu/navigation_menu" />   

阅读NavigationView了解更多详情。


推荐阅读