自定义通知:java.lang.RuntimeException的:坏数组的长度 [英] Custom notification: java.lang.RuntimeException: bad array lengths
问题描述
我试图用使用谷歌的距离样品这里在我的Android应用程序的自定义通知一>(第<一href="http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView">Creating自定义通知布局)。
自从我使用这个确切的code我有一个的RuntimeException
每隔几次通知解雇了。
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.
我的堆栈跟踪:
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)
我的code:
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);
...
}
如果我更换一个标准的自定义通知(与 setLatestEventInfo()
)我没有问题。
If I replace the custom notification with a standard one (with setLatestEventInfo()
) I have no problems.
推荐答案
好了,我终于找到了解决办法。 不能重复使用同一个远程视窗的对象像我一样! 我创建了远程视窗中的onCreate()方法,然后我设置其属性在notifyUpdateProgress()。 如果我创建notifyUpdateProgress(对象),只是使用它之前,我不再有例外。
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.
这篇关于自定义通知:java.lang.RuntimeException的:坏数组的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!