如何解决:android.app.RemoteServiceException:从包中发布的错误通知*:无法创build图标:StatusBarIcon

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

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

从我看到的其他答案 – 我看到exception发生时, setSmallIcon()没有正确调用。

我已经检查并仔细检查了传递的资源ID是否都是正确的。

发生了什么事情,我在PendingIntent包中包含了对该图标的整数引用,并且该整数在被发布到NotificationManager时被引用。

在获取整数引用和待处理的意图之间,应用程序已更新,并且所有可绘制引用都已更改。 现在用来引用正确的drawable的整数引用了不正确的drawable或根本没有(根本不引起这个崩溃)

在通知中使用VectorXml已知会导致此问题。 使用PNG的

android.app.RemoteServiceException:发布错误的通知

我有同样的问题,但我解决了。 我的问题是远程视图的“.xml文件”。

在我的xml文件中,我在LinearLayout之间为分隔符添加了一个View

 <View android:layout_width="match_parent" android:layout_height="1dp" android:id="@+id/view" android:background="#000000" /> 

上面的View组件创buildBad通知exception。 这个exception的原因只是Remoteviews的xml文件。

删除该视图组件后,我的代码正确执行,没有任何例外。 所以我觉得通知抽屉不接受任何定制的意见。

所以你不要在RemoteView对象的.xml文件中绘制上述视图。

在我的应用程序中,这种错误只在升级过程中发生。 如果资源ID在较新版本中发生变化,则Android RemoteView可能无法find资源并抛出RemoteServiceException 。 如果您发布第三个版本并且不更改资源ID,则这些错误可能只会暂时消失。

可以通过编辑res/values/public.xmlres/values/ids.xml来减less这种错误。 如果资源ID不在public.xmlids.xml编译器将生成单个资源ID。 当您更改资源名称或添加一些新资源时,ID可能会更改,并且某些设备可能无法find它。

所以步骤如下:

  1. 反编译apk文件并在res/valuesfindpublic.xmlids.xml
  2. 在应用程序中查找与RemoteView相关的所有资源,并将其复制(string,维度,可绘制,布局,标识,颜色…)
  3. 在源代码的res/values下创buildpublic.xmlids.xml ,并粘贴刚刚复制的行

注意:

Gradle 1.3.0及以上版本忽略了本地的public.xml 。 为了使它工作,你需要在你的build.gradle添加一些脚本

 afterEvaluate { for (variant in android.applicationVariants) { def scope = variant.getVariantData().getScope() String mergeTaskName = scope.getMergeResourcesTask().name def mergeTask = tasks.getByName(mergeTaskName) mergeTask.doLast { copy { int i=0 from(android.sourceSets.main.res.srcDirs) { include 'values/public.xml' rename 'public.xml', (i == 0? "public.xml": "public_${i}.xml") i++ } into(mergeTask.outputDir) } } } } 

注意:此脚本不支持子模块项目。 我正在修复它。

同步项目,然后清理它,File> Synchronize then:Buildt> Clean Project

我希望这会帮助你,这对我有用

每当我想在Kitkat上显示通知时,我都有同样的问题。 是什么导致了我的问题是,我已经定义了每个图标在XML(从SVG),小图标和动作图标也。 在我用png-s代替它们之后,问题在我身边解决了。

我在捆绑中设置通知时遇到了同样的问题。 我试过这个,它解决了我的问题:

 builder.setLargeIcon(large_icon); builder.setSmallIcon(R.drawable.small_icon); 

确保在setSmallIcon()之前调用setLargeIcon()。

我也有同样的问题。 问题是你正在使用的图标,我正在使用android.R.drawable.stat_sys_download。 转到drawables.xml并将其粘贴。

 <resources> <item name="ic_start_download" type="drawable">@android:drawable/stat_sys_download</item> </resource> 

然后在你的代码中

 builder.setSmallIcon(R.drawable.ic_start_download); 

你可以尝试一些其他的图像,而不是这个图像

我的方式来解决这个问题:我的布局(例如一个checkbox)有“坏”的意见 – 所以我删除了它们。

RemoteViews似乎只支持图像和文本(通过阅读文档来确认)。

当使用android.support.constraint.ConstraintLayout时,我有RemoteServiceException。 将其更改为LinearLayout或Relative,还将android:layout_height =“wrap_content”更改为容器