如何修复“错误夸大类android.support.v4.widget.DrawerLayout”

时间:2019-08-15 02:30:42

标签: android inflate-exception

在将应用程序部署到手机时,该应用程序继续崩溃,并显示一条消息,表明该应用程序已停止。 我已经尝试过寻找堆栈溢出的答案,但是没有任何方法可以解决我的问题。我清理了我的项目,“无效并重新启动”,将Api从28更改为27,答案之一也是这样说的。一直以来,我都竭尽所能。

MainActivity.java

IfNoneMatch

build.gradle

ifClosed

activity_main.xml:

 package com.example.panchaat;
 import android.content.Intent;
 import android.os.Bundle;
 import androidx.appcompat.widget.Toolbar;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.*;
 import androidx.annotation.NonNull;
 import androidx.appcompat.app.ActionBarDrawerToggle;
 import androidx.appcompat.app.AppCompatActivity;
 import androidx.drawerlayout.widget.DrawerLayout;
 import androidx.recyclerview.widget.RecyclerView;
 import com.google.android.material.navigation.NavigationView;
 import com.google.firebase.auth.FirebaseAuth;
 import com.google.firebase.auth.FirebaseUser;
 public class MainActivity extends AppCompatActivity {
    private NavigationView navigationView;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private DrawerLayout drawerLayout;
    private RecyclerView postList;
    private Toolbar mToolbar;
    private FirebaseAuth mAuth;
 @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
                      }
                     }

app_bar_layout.xml:

      apply plugin: 'com.android.application'

 android {
 compileSdkVersion 28
 defaultConfig {
    applicationId "com.example.panchaat"
    minSdkVersion 19
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner 
  "android.support.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
 }
 buildTypes {
     release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 
 'proguard-rules.pro'
      }
  }
  compileOptions {
       targetCompatibility = '1.8'
       sourceCompatibility = '1.8'
  }
   buildToolsVersion = '28.0.3'
 }

 dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'com.android.support:appcompat-v7:28.1.1'
   implementation 'com.android.support:design:28.1.0'
   implementation 'com.android.support.constraint:constraint-layout:1.1.2'
   implementation 'com.google.firebase:firebase-auth:11.0.2'
   implementation 'com.google.android.gms:play-services-auth:11.0.2'
   implementation 'com.google.firebase:firebase-database:11.0.2'
   implementation 'com.google.firebase:firebase-storage:11.0.2'
   implementation 'de.hdodenhof:circleimageview:3.0.0'
   implementation 'com.theartofdev.edmodo:android-image-cropper:+'
   implementation 'com.squareup.picasso:picasso:2.5.2'
   implementation 'com.firebaseui:firebase-ui-database:2.1.1'
   implementation 'androidx.appcompat:appcompat:1.0.2'
   implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
   implementation 'com.android.support:appcompat-v7:28.1.1'
   implementation 'com.android.support:design:28.1.1'
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
   androidTestImplementation 'com.android.support.test.espresso:espresso- 
   core:3.0.2'
   implementation 'com.android.support:support-v13:28.0.0'
   implementation files('support-v13')
   implementation 'com.android.support:support-v4:28.0.0'
   implementation 'com.android.support:support-v13:28.0.0'
   implementation "com.android.support:support-core-utils:28.1.0"
   }
   apply plugin: 'com.google.gms.google-services'

logcat

<?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:fitsSystemWindows ="true"
    tools:context="com.example.panchaat.MainActivity"
    android:id="@+id/drawer_layout">

<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:layout_width="match_parent"
            android:layout_height="634dp"
            android:layout_alignParentStart="true"
            android:layout_marginStart="0dp"
            android:layout_alignParentTop="true"
            android:layout_marginTop="21dp"
            android:id="@+id/main_contaier">

        <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/all_users_post_list"
                android:layout_alignTop="@+id/main_contaier"
                android:layout_alignStart="@+id/main_contaier"/>
    </FrameLayout>
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layout_marginBottom="3dp"
        app:menu="@menu/navigation_menu"
        android:id="@+id/navigation_view">
 </com.google.android.material.navigation.NavigationView>
 </androidx.drawerlayout.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:0)

Caused by: android.view.InflateException: Binary XML file line #2: Binary 
 XML file line #2: Error inflating class 
 android.support.v4.widget.DrawerLayout
 Caused by: android.view.InflateException: Binary XML file line #2: Error 
 inflating class android.support.v4.widget.DrawerLayout
 Caused by: java.lang.ClassNotFoundException: Didn't find class 
 "android.support.v4.widget.DrawerLayout"

这是因为您要在activity_main.xml混合使用Android支持库和AndroidX

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

 <!-- This is Android Support Library -->
 <android.support.v4.widget.DrawerLayout
    ...
    android:id="@+id/drawable_layout">

    ...

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

</android.support.v4.widget.DrawerLayout>

似乎您的依赖项中没有使用任何支持库。因此会引发错误。

因此,更改布局以使用AndroidX视图。

对于您的activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.panchaat.MainActivity"
    android:id="@+id/drawable_layout">

     ...

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

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

</androidx.drawerlayout.widget.DrawerLayout>

针对您的app_bar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
     android:id ="@+id/main_app_bar"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background ="#3a1c71"
    android:theme ="@style/ThemeOverlay.AppCompat.Light">

</androidx.appcompat.widget.Toolbar>

并确保您还在Activity中导入正确的对应视图。

请访问Migrating to AndroidX