You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android Studio中App崩溃:Button加载失败及资源异常求助

Fixing Your App Crash: Error Inflating Class Button

Hey there, let's break down why your app is crashing right after launch and get it fixed up. Looking at your error logs and layout file, here's the root cause and step-by-step solutions:

What's Triggering the Crash?

Your Logcat output highlights two critical issues that are causing the crash:

Caused by: android.content.res.Resources$NotFoundException: Drawable android:color/tertiary_text_light with resource ID #0x1060011

Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #18: tag requires a 'drawable' attribute or child tag defining a drawable

In plain terms:

  1. Your app is trying to use an outdated system color resource (android:color/tertiary_text_light) that's no longer supported in your target SDK version.
  2. The res/color/tertiary_text_light.xml file has invalid syntax—specifically, an <item> tag that doesn't properly define a drawable resource.

Step-by-Step Fixes

1. Repair the Invalid Drawable File

Open res/color/tertiary_text_light.xml and navigate to line 18. Ensure every <item> tag in the selector includes an android:drawable attribute, or a valid drawable child element. A correct state list drawable looks like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Pressed state -->
    <item android:state_pressed="true" android:drawable="@color/button_pressed" />
    <!-- Default state -->
    <item android:drawable="@color/button_normal" />
</selector>

If this file was auto-generated or you don't explicitly need it, just delete it entirely—this will force your app to use AppCompat-compatible resources instead.

2. Replace Outdated System Color References

android:color/tertiary_text_light is an old system resource that's not supported in modern AppCompat themes. Update any references to it:

  • If your Button's text color or background uses this resource directly, replace it with an AppCompat-compatible color (like @color/abc_secondary_text_material_light) or a custom color you define in res/values/colors.xml.
  • Check your app's theme in res/values/styles.xml—make sure you're using a Theme.AppCompat subclass (e.g., Theme.AppCompat.Light) instead of an old system theme. This ensures all UI components rely on supported resources.

3. Clean and Rebuild Your Project

Cached old resources can sometimes cause issues even after you fix the underlying files. Do this:

  • Go to Build > Clean Project in Android Studio
  • Then run Build > Rebuild Project
  • Restart Android Studio and launch your app again

4. Explicitly Set Button Properties (Optional)

If the issue stems from your theme's default Button styling, you can override it directly in your layout to avoid relying on problematic resources. Update your Button like this:

<Button
    android:id="@+id/btn_loadData"
    android:layout_width="172dp"
    android:layout_height="43dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="64dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:text="Load Data"
    <!-- Explicitly set text color and background -->
    android:textColor="@android:color/primary_text_light"
    android:background="@drawable/your_custom_button_background"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.506"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/eT_surname" />

Final Notes

This crash is a common hiccup when mixing old system resources with modern AppCompat libraries. Following these steps should resolve the inflation error and get your app running smoothly again.

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

火山引擎 最新活动