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

Android新手遇布局加载异常:无法启动活动,XML解析错误求助

Fixing Android InflateException with Support Library & Third-Party View Issues

Hey there, let's work through this InflateException you're hitting—it's a super common snag for new Android devs, especially when mixing support libraries and third-party UI components. Let's break down the possible causes and fixes step by step:

First, Fix Duplicate & Inconsistent Dependencies

Looking at your dependency list, you've got duplicate entries for com.android.support:design:26.+—that's already a red flag. Duplicate dependencies can cause version conflicts that break layout inflation. Here's what to do first:

  1. Remove the duplicate compile 'com.android.support:design:26.+' line, leaving only one instance.
  2. Stop using the + wildcard for support library versions. Wildcards can pull in unexpected updates that clash with other libraries. Instead, use a specific stable version (like 26.1.0, which is compatible with SDK 26):
    compile 'com.android.support:design:26.1.0'
    compile 'com.android.support:appcompat-v7:26.1.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.cepheuen.elegant-number-button:lib:1.0.2'
    
  3. Make sure your module-level build.gradle has compileSdkVersion 26 set—your support library version must match your compile SDK version.

Resolve Third-Party Library Dependency Conflicts

The elegant-number-button library might be pulling in a different version of the Android support libraries than you're using, which causes inflation failures. To fix this:

  • Run the dependency tree command to check for conflicts:
    # On macOS/Linux
    ./gradlew app:dependencies
    # On Windows
    gradlew.bat app:dependencies
    
  • Look for entries under com.cepheuen.elegant-number-button:lib:1.0.2 that reference com.android.support libraries. If their version doesn't match your 26.1.0 setup, exclude those conflicting dependencies:
    compile ('com.cepheuen.elegant-number-button:lib:1.0.2') {
        exclude group: 'com.android.support'
    }
    

Verify Your Layout File (Line 49)

The error points to line 49 of your XML layout, where it can't inflate an "unknown class." This is almost certainly the ElegantNumberButton component. Double-check:

  1. You're using the full class name in your XML:
    <com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
        android:id="@+id/quantity_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:initialNumber="1"
        app:finalNumber="10" />
    
  2. Your root layout has the app namespace declared:
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto" <!-- This line is critical -->
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    

Clean & Rebuild Your Project

Sometimes cached build files cause weird inflation issues. Do a full clean and rebuild:

  • Go to Build > Clean Project in Android Studio
  • Then run Build > Rebuild Project

If you've tried all these steps and still see the error, share the exact code from line 49 of your XML layout and the full output of the dependency tree command—we can dig deeper from there!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:39:28