自定义通知:java.lang.RuntimeException:错误的数组长度 [英] Custom notification: java.lang.RuntimeException: bad array lengths
问题描述
我正在尝试使用来自 此处(部分创建自定义通知布局).由于我使用的是这个确切的代码,因此每隔几次触发通知就会收到一个 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)
我的代码:
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.
推荐答案
好的,我终于找到了解决方案.
Ok, so I finally find the solution.
您不能像我一样重复使用相同的 RemoteView
对象!
You cannot re-use the same RemoteView
object like I did!
我在 onCreate()
方法中创建了 RemoteView
,然后我在 notifyUpdateProgress()
中设置了它的属性.
I was creating the RemoteView
in the onCreate()
method, then I was setting its attributes in notifyUpdateProgress()
.
如果我在使用它之前在 notifyUpdateProgress()
中创建对象,我就不再有异常了.
If I create the object in notifyUpdateProgress()
just before using it, I'm not having exception anymore.
这篇关于自定义通知:java.lang.RuntimeException:错误的数组长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!