You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio应用无法启动:ActionBarContainer加载异常求助

问题:调整JDK与安卓版本后应用启动崩溃,无法加载ActionBarContainer

我是Android Studio新手,此前为解决需Java 16+版本的问题,尝试集成新JDK并调整安卓版本,代码错误虽已消除,但应用现在完全无法启动,报错如下:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: it.wieland.abclists, PID: 7495
java.lang.RuntimeException: Unable to start activity ComponentInfo{it.test.abclists/it.test.abclists.MainActivity}: android.view.InflateException: Binary XML file line #35 in it.test.abclists:layout/abc_screen_toolbar: Binary XML file line #35 in it.test.abclists:layout/abc_screen_toolbar: Error inflating class androidx.appcompat.widget.ActionBarContainer
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3576)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3748)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2187)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:236)
    at android.app.ActivityThread.main(ActivityThread.java:8057)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)
 Caused by: android.view.InflateException: Binary XML file line #35 in it.test.abclists:layout/abc_screen_toolbar: Binary XML file line #35 in it.test.abclists:layout/abc_screen_toolbar: Error inflating class androidx.appcompat.widget.ActionBarContainer
 Caused by: android.view.InflateException: Binary XML file line #35 in it.test.abclists:layout/abc_screen_toolbar: Error inflating class androidx.appcompat.widget.ActionBarContainer

我甚至找不到名为abc_screen_toolbar的布局,以下是我的配置文件:

Manifest文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.ABCLists"
    tools:targetApi="26">
    <activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

app模块build.gradle

plugins {
id 'com.android.application'
}

android {
namespace 'it.test.abclists'
compileSdk 33

defaultConfig {
    applicationId "it.test.abclists"
    minSdk 26
    targetSdk defaultTargetSdkVersion
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_6
    targetCompatibility JavaVersion.VERSION_1_6
}
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.room:room-common:2.4.2'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
// Room components
implementation "androidx.room:room-runtime:$rootProject.roomVersion"
annotationProcessor "androidx.room:room-compiler:$rootProject.roomVersion"
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"
}

项目根目录build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.4.2' apply false
id 'com.android.library' version '7.4.2' apply false
}
ext {
appCompatVersion = '1.5.1'
constraintLayoutVersion = '2.1.4'
coreTestingVersion = '2.1.0'
lifecycleVersion = '2.3.1'
materialVersion = '1.3.0'
roomVersion = '2.3.0'
// testing
junitVersion = '4.13.2'
espressoVersion = '3.4.0'
androidxJunitVersion = '1.1.2'
defaultTargetSdkVersion = 26
}

请问我哪里操作出错了?该如何解决?


解决方案

问题根源

  1. Java版本配置冲突:你需要使用Java 16+,但compileOptions仍设置为Java 1.6,版本不匹配导致AppCompat库内部布局加载失败;abc_screen_toolbar是AppCompat库内置布局,并非你项目中的文件,报错是库加载异常的表现。
  2. 依赖版本不一致:根目录定义的依赖版本(如appCompatVersion 1.5.1)和app模块中实际使用的版本(1.4.1)不统一,Room库也存在版本混用(2.3.0和2.4.2),引发兼容性问题。

具体修复步骤

1. 修正Java编译版本

打开app模块的build.gradle,修改compileOptions为你需要的Java版本(以Java 17为例,适配Android Studio常用配置):

compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
}

同时确保项目JDK配置为对应版本:File > Project Structure > SDK Location > JDK Location选择Java 17。

2. 统一依赖版本,消除冲突

修改app模块的dependencies,全部使用根目录定义的版本,移除重复依赖:

dependencies {
    implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
    implementation "com.google.android.material:material:$rootProject.materialVersion"
    implementation "androidx.constraintlayout:constraintlayout:$rootProject.constraintLayoutVersion"
    testImplementation "junit:junit:$rootProject.junitVersion"
    androidTestImplementation "androidx.test.ext:junit:$rootProject.androidxJunitVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$rootProject.espressoVersion"
    implementation 'androidx.recyclerview:recyclerview:1.3.2' // 更新为兼容版本
    // Room组件(移除单独的room-common,room-runtime已包含)
    implementation "androidx.room:room-runtime:$rootProject.roomVersion"
    annotationProcessor "androidx.room:room-compiler:$rootProject.roomVersion"
    androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"
}

3. 检查主题继承关系

确保Theme.ABCLists继承自AppCompat或MaterialComponents主题,在res/values/styles.xml中修改:

<style name="Theme.ABCLists" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- 自定义主题属性 -->
</style>

4. 清理重建项目

  • 执行Build > Clean Project
  • 执行Build > Rebuild Project
  • 若使用模拟器,清除其缓存:Settings > System > Advanced > Reset options > Erase all data (factory reset)

内容的提问来源于stack exchange,提问作者Kev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 03:17:16