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

Android Studio 3.0中Error inflating class ImageButton问题排查求助

Fixing "Error inflating class ImageButton" in Android Studio 3.0

Hey there, let's break down why your ImageButton is causing that inflation error and how to fix it:

Possible Causes & Solutions

1. Missing or Misconfigured AppCompat Support Library

The app:srcCompat attribute you're using is part of the AppCompat support library, which requires proper setup:

  • Check your app-level build.gradle: Make sure you have the AppCompat dependency added. For the old support library (common in Android Studio 3.0 projects), add this line in the dependencies block:
    implementation 'com.android.support:appcompat-v7:28.0.0'
    
  • Verify your app theme: Ensure your application uses an AppCompat-based theme in AndroidManifest.xml. Your application tag should have a theme like:
    android:theme="@style/AppTheme"
    
    And in res/values/styles.xml, AppTheme should inherit from an AppCompat theme:
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here -->
    </style>
    

2. Invalid or Missing Drawable Resources

Double-check your image assets:

  • Confirm @drawable/tom and @drawable/jerry exist in your res/drawable (or density-specific folders like drawable-hdpi) directory. Make sure the files are not corrupted and are in a supported format (PNG, JPG, etc.).
  • Ensure your drawable resource names follow Android's rules: only lowercase letters, numbers, and underscores (your tom and jerry names are fine here, but just double-check for typos).

3. Try Using android:src Instead of app:srcCompat

If you don't need the vector drawable support provided by srcCompat right now, you can switch to the standard android:src attribute for your ImageButton:

<ImageButton
    android:id="@+id/cartoon"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:contentDescription="@string/this_is_tom"
    android:onClick="change"
    android:src="@drawable/tom" />

This bypasses the AppCompat-specific attribute and might resolve the inflation issue immediately.

Bonus: Polish Your Animation Code

Once you fix the inflation error, keep an eye on your change method: right now, you set the image resource immediately while the alpha animation is running. If you want the image swap to happen after the animation finishes, use an AnimatorListener:

Tom.animate().alpha(0.3f).setDuration(2000).setListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animator) {}

    @Override
    public void onAnimationEnd(Animator animator) {
        Tom.setImageResource(R.drawable.jerry);
    }

    @Override
    public void onAnimationCancel(Animator animator) {}

    @Override
    public void onAnimationRepeat(Animator animator) {}
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 03:59:01