17

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.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
user1026605
  • 1,633
  • 4
  • 22
  • 58

2 Answers2

23

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.

Brigham
  • 14,395
  • 3
  • 38
  • 48
7

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.

Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40
user1026605
  • 1,633
  • 4
  • 22
  • 58
  • 1
    Sometimes I get this crash even with a brand new RemoteView, so I think it's related to bitmaps as mentioned by @Brigham – tokudu Apr 12 '13 at 21:21
  • @Brigham's answer is correct. You may have been leaking `Bitmap` objects around your `RemoteView`. – Kedar Paranjape Feb 09 '14 at 17:50