Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 903
user1026605 Avatar asked Nov 02 '11 22:11

user1026605


2 Answers

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.

like image 114
Brigham Avatar answered Oct 08 '22 16:10

Brigham


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.

like image 26
user1026605 Avatar answered Oct 08 '22 17:10

user1026605