android - 如何修复 : android. app.RemoteServiceException:从包发布的错误通知 *:无法创建图标:StatusBarIcon

标签 android android-notifications android-resources android-drawable

我在崩溃日志中看到以下异常:

android.app.RemoteServiceException: Bad notification posted from package com.my.package: Couldn't create icon: StatusBarIcon(pkg=com.my.package user=0 id=0x7f02015d level=0 visible=true num=0 )
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1456)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5487)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)

我正在使用以下方法通过 AlarmManager 从 PendingIntent 集中的 IntentService 发布我的通知。这里传入的所有值都来自 PendingIntent/IntentService 中的 bundle extras。

/**
 * Notification 
 *
 * @param c
 * @param intent
 * @param notificationId
 * @param title
 * @param message
 * @param largeIcon
 * @param smallIcon
 */
public static void showNotification(Context c, Intent intent,
        int notificationId, String title, String message, int largeIcon,
        int smallIcon) {
    PendingIntent detailsIntent = PendingIntent.getActivity(c,
            notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // BUILD
    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
            c);
    // TITLE
    mNotifyBuilder.setContentTitle(title).setContentText(message);

    // ICONS
    mNotifyBuilder.setSmallIcon(smallIcon);
    if (Util.isAndroidOSAtLeast(Build.VERSION_CODES.HONEYCOMB)) {
        Bitmap large_icon_bmp = ((BitmapDrawable) c.getResources()
                .getDrawable(largeIcon)).getBitmap();
        mNotifyBuilder.setLargeIcon(large_icon_bmp);
    }

    mNotifyBuilder.setContentIntent(detailsIntent);
    mNotifyBuilder.setVibrate(new long[] { 500, 1500 });
    mNotifyBuilder.setTicker(message);
    mNotifyBuilder.setContentText(message);

    // NOTIFY
    NotificationManager nm = (NotificationManager) c
            .getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(notificationId, mNotifyBuilder.build());
}

根据我看到的其他答案 - 我看到的异常发生在 setSmallIcon() 未正确调用时。

我已经检查并再次检查了传递的资源 ID 是否正确。

最佳答案

发生的事情是,我在 PendingIntent 包中包含了对图标的整数引用,后来在发布到 NotificationManager 时引用了该整数。

在获取整数引用和挂起的 Intent 之间,应用程序被更新并且所有可绘制引用都发生了变化。用于引用正确可绘制对象的整数现在引用了不正确的可绘制对象或根本没有引用(根本没有 - 导致此崩溃)

关于android - 如何修复 : android. app.RemoteServiceException:从包发布的错误通知 *:无法创建图标:StatusBarIcon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25317659/

相关文章:

android - 如何使用 SpannableStringBuilder 缩放多个跨度?

android - ViewPager 停止 Fragment 之间的翻转动画

android - 如何在 android 布局中使用 string.xml 中的字符串数组?

android - 为通知设置自定义声音

android - 在 fragment 中使用资源

java - file_paths.xml 无法从字符串资源读取

android - 如何让android studio高亮显示C代码?

android - ActionBar 外部的自定义溢出菜单

android - 致命异常 : android. app.RemoteServiceException ....无法创建图标:StatusBarIcon

android - 如何在不将其设置为默认值的情况下使用 URL 以编程方式播放通知声音?