【问题标题】:Bad notification posted from package Couldn't expand RemoteViews从包中发布的错误通知无法展开 RemoteViews
【发布时间】:2013-03-26 16:53:34
【问题描述】:

我有问题。有时我的服务会被这个 logcat 强行关闭:

03-26 20:44:44.849: E/AndroidRuntime(12080): FATAL EXCEPTION: main
03-26 20:44:44.849: E/AndroidRuntime(12080): android.app.RemoteServiceException: Bad notification posted from package by.flipdev.vkspy: Couldn't expand RemoteViews for: StatusBarNotification(pkg=by.flipdev.vkspy id=1 tag=null score=0 notn=Notification(pri=0 contentView=by.flipdev.vkspy/0x1090071 vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null]))
03-26 20:44:44.849: E/AndroidRuntime(12080):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1374)
03-26 20:44:44.849: E/AndroidRuntime(12080):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-26 20:44:44.849: E/AndroidRuntime(12080):    at android.os.Looper.loop(Looper.java:137)
03-26 20:44:44.849: E/AndroidRuntime(12080):    at android.app.ActivityThread.main(ActivityThread.java:4931)
03-26 20:44:44.849: E/AndroidRuntime(12080):    at java.lang.reflect.Method.invokeNative(Native Method)
03-26 20:44:44.849: E/AndroidRuntime(12080):    at java.lang.reflect.Method.invoke(Method.java:511)
03-26 20:44:44.849: E/AndroidRuntime(12080):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-26 20:44:44.849: E/AndroidRuntime(12080):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
03-26 20:44:44.849: E/AndroidRuntime(12080):    at dalvik.system.NativeStart.main(Native Method)

这是我添加通知的代码:

protected void addNotification(final Bitmap Avatar,
        final int small_Image_ID, final int notify_id, final String text,
        final String title, final Boolean ongoing, final Boolean ticker,
        final String tickerText, final Boolean autoCancel,
        final String notificationCategory, final int notificationValue) {

    try {

        final Intent notificationIntent = new Intent(
                getApplicationContext(), CheckerActivity.class);

        notificationIntent
                .putExtra(notificationCategory, notificationValue);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        final PendingIntent contentIntent = PendingIntent.getActivity(
                getApplicationContext(), notify_id, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        final NotificationManager nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);


        final NotificationCompat.Builder builder = new NotificationCompat.Builder(
                context);
        if (Ticker) {
            builder.setContentIntent(contentIntent)
                    .setSmallIcon(small_Image_ID)
                    .setOngoing(ongoing)                    
                    .setLargeIcon(Avatar).setTicker(tickerText)                     
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(AutoCancel).setContentTitle(title)
                    .setContentText(text); // Текст уведомления
        } else {
            builder.setContentIntent(contentIntent)
                    .setSmallIcon(small_Image_ID)
                    .setLargeIcon(avatar)                       
                    .setOngoing(ongoing)
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(AutoCancel).setContentTitle(title) 
                    .setContentText(text); // Текст уведомления
        }

        final Notification n = builder.getNotification();

        nm.notify(notify_id, n);

    } catch (final Exception e) {
                    // TODO: add exception handling code
    }
}

为什么我的服务被杀死了?

【问题讨论】:

  • 不知道怎么回事,可能和你的头像位图有关吧?它曾经为空吗?
  • 如果头像图片不为空,您可以检查您的代码吗?
    对于 small_Image_ID,您在哪里分配值?
    如果通知管理器找不到资源,则会触发此类消息:错误通知已发布 XXXX。
    所以首先要检查的是所有值确实存在。
    谢谢
  • 我会说头像为 null 或 small_Image_ID 不引用可绘制对象。
  • 好像头像不为空:contentView=by.flipdev.vkspy/0x1090071

标签: android android-notifications


【解决方案1】:

当我设置大图标时,我注意到 3.0 模拟器上发生了这种情况。

因此,由于大图标仅由 4.0+ 设备使用,因此我通过检查 Build 版本是否 > 13 来解决此问题。如果是(并且仅当是),我将大图标。

问题已经解决了。

【讨论】:

  • 非常有帮助!事实上,builder.setLargeIcon() 在 API 级别 12(至少是模拟器)上崩溃。 (我从来没有能够在 13-14 级模拟器上进行测试,因为它们需要花费大部分时间来启动,然后在它们准备好做任何事情之前就崩溃了。)我之前发现 builder.setNumber() 在API 12 级模拟器。
  • 好了,不用再担心了。根据Dashboard 3.x 设备现在低于全球市场的 0.1%,因此它们甚至不会再上市。无论如何,这就是为什么我只在设备 > 13 (if (Build.VERSION.SDK_INT > 13){...}) 上设置它。
  • @Rotwang,我在 4.4.2 版本中遇到了这个异常。无法追踪。
【解决方案2】:

此问题是由于资源缺失或为空造成的。 查看您的代码,我推断,Ticker (始终为 false) 和 setLargeIcon(avatar)(头像始终为空) 上似乎可能有错误。

你能发布你所有的服务实现吗?

附:请尽量尊重java的命名约定; Ticker、Avatar、AutoCancel 等对象应以小写开头。

【讨论】:

    【解决方案3】:

    好吧,就我而言,我遇到了同样的错误,因为我在我的自定义通知布局中引用了 TextView 的样式定义,其中包含一个 onClick 目标方法的条目。具体来说:

    <item name="android:onClick">onClick</item>
    

    删除那条线后,问题就消失了。这是我的疏忽,但提醒您不要盲目重用样式。

    【讨论】:

      【解决方案4】:

      除了 Google 推荐的布局和小部件 https://developer.android.com/reference/android/widget/RemoteViews

      检查layout.xml 文件,如果您使用android:tint 属性,请将其删除。 因为RemoteViews 不接受layout.xml 和android:tint

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-04
        • 1970-01-01
        • 2018-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多