android.graphics.drawable.AdaptiveIconDrawable cannot be cast to android.graphics.drawable.BitmapDrawable error

醉酒当歌 提交于 2019-12-06 05:19:27

问题


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);

回答1:


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.




回答2:


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()));



回答3:


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.



来源:https://stackoverflow.com/questions/50077917/android-graphics-drawable-adaptiveicondrawable-cannot-be-cast-to-android-graphic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!