【问题标题】:How to fix "Error inflating class android.support.v4.widget.DrawerLayout"如何修复“错误膨胀类 android.support.v4.widget.DrawerLayout”
【发布时间】:2019-08-15 02:30:42
【问题描述】:

在将应用程序部署到手机时,应用程序不断崩溃并显示应用程序已停止的消息。 我已经尝试寻找堆栈溢出的答案,但没有任何解决我的问题。我清理了我的项目,无效并重新启动,将 Api 从 28 更改为 27,因为其中一个答案也这么说。我已经尝试了所有我能找到的东西。

MainActivity.java

 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);
                      }
                     }

build.gradle

      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'

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"
    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>

app_bar_layout.xml:

<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.Dark">
  </androidx.appcompat.widget.Toolbar>

日志猫

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"

【问题讨论】:

    标签: android inflate-exception


    【解决方案1】:
    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

    【讨论】:

    • 我按照你说的做了修改,但是问题没有得到解决
    • 引起:android.view.InflateException:二进制 XML 文件第 2 行:二进制 XML 文件第 2 行:错误膨胀类 androidx.support.v4.widget.DrawerLayout
    • @NabeelAhmed:你的 xml 有点不对,应该是androidx.drawerlayout.widget.DrawerLayout
    • 请查看我编辑的问题,我已经包含了我在 MainActivity 中所做的导入,还包含了 build.gradle 文件以及您之前提到的版本。现在我应该怎么做才能解决这个问题。 TIA
    猜你喜欢
    • 2014-12-12
    • 2019-07-05
    • 2017-01-17
    • 2020-02-06
    • 1970-01-01
    • 2012-06-02
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多