android.graphics.drawable.AdaptiveIconDrawable无法强制转换为android.graphics.drawable.BitmapDrawable错误

时间:2018-04-28 15:06:54

标签: android android-8.0-oreo

我的应用在启动时崩溃时尝试实施自适应图标时出现此错误。

我无法想象为什么我会收到此错误,因为在下面的错误日志(MainActivityBaseActivity)中提到的文件中,我没有使用AdaptiveIconDrawableBitmapDrawable

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)

"搞笑"出现此错误消息的部分是当我删除" mipmap-anydpi-v26"文件夹,我的应用程序不再崩溃,但随后自适应图标也不起作用......

在两种情况下都会抛出错误的行:

super.onCreate(savedInstanceState);

3 个答案:

答案 0 :(得分:6)

在ATE.java文件中,实现此

@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;
}

然后改变

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

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

答案 1 :(得分:3)

您似乎正在使用App Theme Engine,它会尝试将您的应用图标用作位图。但是从Android 8.0开始,您可以设置一个自适应图标,这不是位图,因此App Theme Engine会崩溃。

这个问题一直是reported here,但它尚未得到解决(可能不会被解决,因为它已经两年没有看到任何更新)。

您可以通过分叉项目并将problematic part更改为不需要位图图标来自行解决此问题。或者,使用另一个主题引擎。

答案 2 :(得分:3)

我也面临着同样的错误。这是由于openDetail(variant){ this.buttonColor = '#345465'; } 自动创建了ic_launcher。我只是删除自适应AdaptiveIcon并运行,所以它可以正常工作。