2011-11-02 30 views
16

Tôi đang cố gắng sử dụng thông báo tùy chỉnh trong ứng dụng Android của mình bằng mẫu của Google từ here (phần Creating a Custom Notification Layout). Vì tôi đang sử dụng mã chính xác này, tôi đã nhận được RuntimeException mỗi vài lần thông báo được kích hoạt.Thông báo tùy chỉnh: java.lang.RuntimeException: độ dài mảng xấu

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) 

Mã của tôi:

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

     ... 
     }  

Nếu tôi thay thế các thông báo tùy chỉnh với một tiêu chuẩn (với setLatestEventInfo()) Tôi không có vấn đề.

Trả lời

5

Ok, vì vậy cuối cùng tôi đã tìm ra giải pháp. Bạn không thể sử dụng lại cùng một đối tượng RemoteView như tôi đã làm! Tôi đã tạo RemoteView trong phương thức onCreate(), sau đó tôi đã thiết lập các thuộc tính của nó trong notifyUpdateProgress(). Nếu tôi tạo đối tượng trong notifyUpdateProgress() ngay trước khi sử dụng nó, tôi không có ngoại lệ nữa.

+1

Đôi khi tôi gặp sự cố này ngay cả với một RemoteView hoàn toàn mới, vì vậy tôi nghĩ rằng nó liên quan đến bitmap như được đề cập bởi @Brigham – tokudu

+0

@ Câu trả lời của Brigham là chính xác. Bạn có thể đã bị rò rỉ các đối tượng 'Bitmap' quanh' RemoteView' của bạn. –

18

Nếu bạn gặp sự cố này, hãy điều tra kích thước của Bitmap mà bạn đang sử dụng cho hình ảnh thông báo.

Nếu quá lớn, bạn có thể thấy lỗi này, cũng như OutOfMemoryError ở phía hệ thống Android.

+0

m không sử dụng bất kỳ hình ảnh nào ngoài biểu tượng ứng dụng được đặt trong bố cục – DjHacktorReborn

Các vấn đề liên quan