android.graphics.drawable.AdaptiveIconDrawable can

2020-07-18 05:41发布

I'm getting this error when trying to implement Adaptive Icons while my app crashes on startup.

I can't imagine why I'm getting this error because in the files mentioned in the error log below (MainActivity and BaseActivity), I don't use both AdaptiveIconDrawable and BitmapDrawable.

2018-04-28 16:50:17.014 31282-31282/de.markustippner.wondermusic2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: de.markustippner.wondermusic2, PID: 31282
    java.lang.RuntimeException: Unable to start activity ComponentInfo{de.markustippner.wondermusic2/de.markustippner.wondermusic2.activities.MainActivity}: java.lang.ClassCastException: android.graphics.drawable.AdaptiveIconDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:101)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:73)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1786)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
     Caused by: java.lang.ClassCastException: android.graphics.drawable.AdaptiveIconDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
        at com.afollestad.appthemeengine.ATE.applyTaskDescription(ATE.java:259)
        at com.afollestad.appthemeengine.ATE.preApply(ATE.java:128)
        at com.afollestad.appthemeengine.ATEActivity.onCreate(ATEActivity.java:22)
        at de.markustippner.wondermusic2.activities.BaseActivity.onCreate(BaseActivity.java:44)
        at de.markustippner.wondermusic2.activities.MainActivity.onCreate(MainActivity.java:137)
        at android.app.Activity.performCreate(Activity.java:7117)
        at android.app.Activity.performCreate(Activity.java:7108)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1262)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2867)

The "funny" part with this error message is that when I remove "mipmap-anydpi-v26" folder, my app doesn't crash anymore but then Adaptive Icons are not working too...

The lines where the error is thrown are in both cases:

super.onCreate(savedInstanceState);

3条回答
贪生不怕死
2楼-- · 2020-07-18 06:22

I am also facing same error. It's due to ic_launcher creating automatically AdaptiveIcon. I just remove adaptive icon(ic_launcher.xml) and run, so it's working.

查看更多
▲ chillily
3楼-- · 2020-07-18 06:24

In ATE.java file, implement this

@NonNull
static private Bitmap getBitmapFromDrawable(@NonNull Drawable drawable) {
    final Bitmap bmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bmp);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bmp;
}

And change

if (icon == null)
        icon = ((BitmapDrawable) activity.getApplicationInfo().loadIcon(activity.getPackageManager())).getBitmap();

to

if (icon == null)
        icon = getBitmapFromDrawable(activity.getApplicationInfo().loadIcon(activity.getPackageManager()));
查看更多
Deceive 欺骗
4楼-- · 2020-07-18 06:45

It appears you're using App Theme Engine, which tries to use you app icon as a bitmap. But since Android 8.0 you can set an adaptive icon, which is not a bitmap, thus the App Theme Engine crashes on it.

This issue has been reported here, but it's not been fixed yet (and probably won't, since it hasn't seen any updates for two years).

You might be able so fix this yourself by forking the project and changing the problematic part to not require a bitmap icon. Or alternatively, use another theme engine.

查看更多
登录 后发表回答