安卓App启动崩溃:Error inflating class AppBarLayout
Hey there, let's get your celebrity guess app up and running again! The crash you're seeing is caused by a missing dependency—your app can't find the AppBarLayout class because the Design Support Library (which contains this component) isn't included in your project. Let's walk through the fixes step by step:
Step 1: Add the Design Support Library Dependency
Open your app-level build.gradle file (usually located at app/build.gradle) and add the following line to the dependencies block:
implementation 'com.android.support:design:28.0.0'
Important: Make sure this version number matches the version of your other Support Library dependencies (like com.android.support:appcompat-v7—it should be the same, e.g., 28.0.0).
Step 2: Sync Your Gradle Project
Click the Sync Now button that appears in the top-right corner of Android Studio after modifying the build.gradle file. This will download the library and add it to your project.
Step 3: Clean & Rebuild (If Needed)
If you still see the error after syncing, try cleaning and rebuilding your project to clear cached build files:
- Go to
Build > Clean Project - Then go to
Build > Rebuild Project
Quick Validation Checks
- Your
activity_main.xmluses the correct full class name forAppBarLayout(android.support.design.widget.AppBarLayout)—which it does, so that's already set correctly. - Your app's theme extends
Theme.AppCompat(yours usesTheme.AppCompat.Light.DarkActionBar), which is required for Design Support Library components to function properly.
Once you've completed these steps, your app should inflate the AppBarLayout without crashing, and you can get back to refining your celebrity guess game!
内容的提问来源于stack exchange,提问作者Minh

