Custom notification: java.lang.RuntimeException: bad array lengths

匿名 (未验证) 提交于 2019-12-03 08:33:39

问题:

I'm trying to use custom notifications in my Android application using Google's sample from here (section Creating a Custom Notification Layout). Since I'm using this exact code I got a RuntimeException every few times a notification is fired.

My stack trace:

FATAL EXCEPTION: main java.lang.RuntimeException: bad array lengths     at android.os.Parcel.readIntArray(Parcel.java:677)     at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:369)     at android.app.NotificationManager.notify(NotificationManager.java:110)     at android.app.NotificationManager.notify(NotificationManager.java:90)     at com.****.service.UpdateFeedService.notifyUpdateProgress(UpdateFeedService.java:266)     at com.****.service.task.PodcastUpdaterTask.onProgressUpdate(PodcastUpdaterTask.java:63)     at com.****.service.task.PodcastUpdaterTask.onProgressUpdate(PodcastUpdaterTask.java:1)     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:432)     at android.os.Handler.dispatchMessage(Handler.java:99)     at android.os.Looper.loop(Looper.java:144)     at android.app.ActivityThread.main(ActivityThread.java:4937)     at java.lang.reflect.Method.invokeNative(Native Method)     at java.lang.reflect.Method.invoke(Method.java:521)     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)     at dalvik.system.NativeStart.main(Native Method) 

My code:

private final Intent updateInProgressIntent = new Intent(this.context, PodcastListActivity.class); private RemoteViews updateInProgressContentView = null; private PendingIntent updateInProgressPendingIntent = null; private Notification updateInProgressNotification = null; ...     @Override      public void onCreate() {          super.onCreate();     ...         this.updateInProgressPendingIntent = PendingIntent.getActivity(this, UPDATE_INPROGRESS_NOTIFICATION_ID,          this.updateInProgressIntent, PendingIntent.FLAG_UPDATE_CURRENT);              this.updateInProgressContentView = new RemoteViews(getPackageName(), R.layout.custom_notification); ...     }      public void notifyUpdateProgress(final int index, final int size, final Podcast podcast) {          this.updateInProgressContentView.setImageViewBitmap(                 R.id.image, ActivityHelper.getBitmap(context, podcast.getThumbnailAsset()));         this.updateInProgressContentView.setTextViewText(R.id.title, "some msg");         this.updateInProgressContentView.setTextViewText(R.id.text, podcast.getName());         this.updateInProgressNotification.contentView = this.updateInProgressContentView;                this.updateInProgressNotification.contentIntent = this.updateInProgressPendingIntent;                        this.notificationManager.notify(UPDATE_INPROGRESS_NOTIFICATION_ID, this.updateInProgressNotification);          ...         }        

If I replace the custom notification with a standard one (with setLatestEventInfo()) I have no problems.

回答1:

If you're seeing this issue, investigate the size of the Bitmap that you're using for the notification image.

If it's too large you may see this error, as well as an OutOfMemoryError on the Android system side.



回答2:

Ok, so I finally find the solution.

You cannot re-use the same RemoteView object like I did!

I was creating the RemoteView in the onCreate() method, then I was setting its attributes in notifyUpdateProgress().

If I create the object in notifyUpdateProgress() just before using it, I'm not having exception anymore.



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