英文:
upgrading app getting Error inflating class com.google.android.material.navigation.NavigationView
问题
**错误信息**
E/AndroidRuntime: 致命异常: 主线程
进程: com.example.bagexpres, 进程ID: 8603
java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.bagexpres/com.example.bagexpres.Main2Activity}: android.view.InflateException: 二进制 XML 文件第 59 行: 二进制 XML 文件第 59 行: 在 com.google.android.material.navigation.NavigationView 中膨胀错误
...(以下为堆栈跟踪)
**XML 布局**
布局的 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=".Main2Activity"
android:id="@+id/drawerLayout">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 其他布局元素 -->
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="@layout/layout_navigation_header"
app:menu="@menu/navigation_menu"
app:itemTextColor="@color/bPrimaryDark"
android:layout_gravity="start"/>
</androidx.drawerlayout.widget.DrawerLayout>
**导航菜单**
导航菜单如下
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menuProfile"
android:title="@string/profile"/>
<!-- 其他菜单项 -->
</menu>
**导航头部布局**
导航头部布局如下
<?xml version="1.0" encoding="utf-8"?>
<ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
android:padding="20dp">
<!-- 其他布局元素 -->
</ConstraintLayout>
**Gradle 配置**
Gradle 配置如下
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.example.bxpr"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
multiDexEnabled true
// 其他配置项
}
buildTypes {
release {
// 其他发布配置项
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
// 其他依赖项
implementation 'com.android.support:multidex:1.0.3'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.1'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.1'
implementation 'com.google.android.material:material:1.3.0-alpha03'
implementation 'com.makeramen:roundedimageview:2.3.0'
}
请注意,上述内容只是您提供的信息的翻译,可能不包含任何额外的解释或回答。如有需要,请随时提问。
英文:
I am trying to convert an old project and getting this error, have wasted so much time but not getting anywhere really appreciate if someone can help. Thanks in advance
Error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bagexpres, PID: 8603
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bagexpres/com.example.bagexpres.Main2Activity}: android.view.InflateException: Binary XML file line #59: Binary XML file line #59: Error inflating class com.google.android.material.navigation.NavigationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2835)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1615)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6687)
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:810)
Caused by: android.view.InflateException: Binary XML file line #59: Binary XML file line #59: Error inflating class com.google.android.material.navigation.NavigationView
Caused by: android.view.InflateException: Binary XML file line #59: 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 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
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.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.example.bagexpres.Main2Activity.onCreate(Main2Activity.java:25)
at android.app.Activity.performCreate(Activity.java:7028)
at android.app.Activity.performCreate(Activity.java:7019)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2788)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1615)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6687)
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:810)
Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class ConstraintLayout
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class ConstraintLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.ConstraintLayout" on path: DexPathList[[zip file "/data/app/com.example.bagexpres-60BGCJxbDuEGgGkaZ5XejQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.bxpr-60BGCJxbDuEGgGkaZ5XejQ==/lib/arm, /system/lib, /system/vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:606)
E/AndroidRuntime: at android.view.LayoutInflater.onCreateView(LayoutInflater.java:703)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.google.android.material.internal.NavigationMenuPresenter.inflateHeaderView(NavigationMenuPresenter.java:222)
at com.google.android.material.navigation.NavigationView.inflateHeaderView(NavigationView.java:387)
at com.google.android.material.navigation.NavigationView.<init>(NavigationView.java:241)
at com.google.android.material.navigation.NavigationView.<init>(NavigationView.java:122)
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 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
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.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.example.bagexpres.Main2Activity.onCreate(Main2Activity.java:25)
at android.app.Activity.performCreate(Activity.java:7028)
at android.app.Activity.performCreate(Activity.java:7019)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2788)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1615)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6687)
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:810)
XML As Below XML of my layout is as below
<?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=".Main2Activity"
android:id="@+id/drawerLayout">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layouttoolbar"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@color/bPrimary"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingStart="15dp"
android:paddingEnd="15dp"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageMenu"
android:layout_width="40dp"
android:layout_height="40dp"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_menu"
android:backgroundTint="@color/bWhite"/>
<TextView
android:id="@+id/textTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:text="@string/app_name"
android:textColor="@color/bWhite"
android:textSize="18sp"
android:textStyle="bold"/>
</LinearLayout>
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/layouttoolbar"
app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/navhostfragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/main"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="@layout/layout_navigation_header"
app:menu="@menu/navigation_menu"
app:itemTextColor="@color/bPrimaryDark"
android:layout_gravity="start"/>
</androidx.drawerlayout.widget.DrawerLayout>
Navigation Menu Navigation Menu is as under
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menuProfile"
android:title="@string/profile"/>
<item
android:id="@+id/menuNotification"
android:title="@string/notification"/>
<item
android:id="@+id/menuBookNow"
android:title="@string/booknow"/>
<item
android:id="@+id/menuOrder"
android:title="@string/order"/>
<item
android:id="@+id/menuPayments"
android:icon="@drawable/ic_payment"
android:title="@string/account_balance"/>
<item android:title="@string/others">
<menu>
<item
android:id="@+id/menuSetting"
android:title="@string/setting"/>
<item
android:id="@+id/menuSupport"
android:title="@string/support"/>
<item
android:id="@+id/menuLogout"
android:title="@string/logout"/>
</menu>
</item>
</menu>
Navigation Header Navigation layout as below
<?xml version="1.0" encoding="utf-8"?>
<ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
android:padding="20dp">
<RoundedImageView
android:id="@+id/imageProfile"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="@drawable/splash2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/viewSupporter"
android:layout_width="1dp"
android:layout_height="1dp"
app:layout_constraintBottom_toBottomOf="@id/imageProfile"
app:layout_constraintEnd_toEndOf="@id/imageProfile"
app:layout_constraintStart_toStartOf="@id/imageProfile"
app:layout_constraintTop_toTopOf="@+id/imageProfile" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:hint="Username"
android:textColor="@color/bPrimary"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@id/viewSupporter"
app:layout_constraintStart_toEndOf="@id/imageProfile"
android:id="@+id/username"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="@string/verified"
android:textColor="@color/bGreen"
android:textSize="16sp"
app:layout_constraintStart_toEndOf="@id/imageProfile"
app:layout_constraintTop_toBottomOf="@id/viewSupporter" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/bGreen"
android:foregroundGravity="bottom"
android:paddingBottom="10dp"
app:layout_constraintTop_toBottomOf="@id/imageProfile" />
</ConstraintLayout>
Gradle this is my gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.example.bxpr"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
implementation 'com.google.firebase:firebase-auth:19.4.0'
implementation 'com.google.firebase:firebase-firestore:21.7.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.android.support:multidex:1.0.3'
//Navigation Component
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.1'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.1'
// Material Design
implementation 'com.google.android.material:material:1.3.0-alpha03'
// Rounded Image View
implementation 'com.makeramen:roundedimageview:2.3.0'
}
答案1
得分: 1
> 由以下原因引起:java.lang.ClassNotFoundException: 没有找到类"android.view.ConstraintLayout"
问题出在您的 layout/layout_navigation_header
中。
请使用
androidx.constraintlayout.widget.ConstraintLayout
替代
ConstraintLayout
英文:
> Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.ConstraintLayout"
The issue is in your layout/layout_navigation_header
Use
androidx.constraintlayout.widget.ConstraintLayout
instead of
ConstraintLayout
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论