我正在尝试创建一个能够设置多个警报的警报应用程序,但是我需要向onReceive发送一个自定义数组列表,问题是它会抛出...
java.lang.RuntimeException: Parcel: unable to marshal value id:7, type:kitchen, wkt:LINESTRING (11.6999998092651 8.40000057220459, 11.4499998092651 9.85000038146973, 10.6500005722046 10, 8.5 9.19999980926514, 9.15000057220459 8.90000057220459, 10.9499998092651 9.25, 11.6999998092651 8.40000057220459), centerX:10.400001,centerY:9.266667, owner:null, keyword:Kitchen, subMap:0
at android.os.Parcel.writeValue(Parcel.java:1418)
at android.os.Parcel.writeList(Parcel.java:759)
at android.os.Parcel.writeValue(Parcel.java:1365)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
at android.os.Bundle.writeToParcel(Bundle.java:1079)
at android.os.Parcel.writeBundle(Parcel.java:711)
at android.content.Intent.writeToParcel(Intent.java:7801)
at android.os.Parcel.writeTypedArray(Parcel.java:1254)
at android.app.ActivityManagerProxy.getIntentSender(ActivityManagerNative.java:4070)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:531)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:515)
at com.asus.robot.onzenbo.dbAlarmManager.startAlarm(dbAlarmManager.java:327)
at com.asus.robot.onzenbo.dbAlarmManager.onTimeSet(dbAlarmManager.java:315)
at com.asus.robot.onzenbo.dbAlarmManager.TimeReceive(dbAlarmManager.java:170)
at com.asus.robot.onzenbo.dbAlarmManager$2.onSuccess(dbAlarmManager.java:146)
at com.asus.robot.onzenbo.dbAlarmManager$2.onSuccess(dbAlarmManager.java:129)
at com.google.android.gms.tasks.zzn.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5444)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
问题似乎是由未决的Intent引起的。
private void startAlarm(ArrayList<Long> aTime) {
AlarmManager[] alarmManagers = new AlarmManager[20];
ArrayList<PendingIntent> intentArrayList = new ArrayList<>();
for (int i = 0 ; i<aTime.size(); i++)
{
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("LOC", arrayListRooms);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,i,intent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManagers[i] = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManagers[i].set(AlarmManager.RTC_WAKEUP, aTime.get(i),pendingIntent);
intentArrayList.add(pendingIntent);
}
}
这是自动生成的读取宗地,除了arrayListRooms(我的自定义arraylist从方法返回)之外。
protected dbAlarmManager(Parcel in) {
super(robotCallback,robotListenCallback);
location = in.createStringArrayList();
time = in.createStringArrayList();
sTime = in.readString();
sLoc = in.readString();
sTitle = in.readString();
sFirstRoom = in.readString();
sSecondRoom = in.readString();
aTitle = in.createStringArrayList();
intent = in.readParcelable(Intent.class.getClassLoader());
hour = in.createIntArray();
min = in.createIntArray();
arrayListRooms = in.createTypedArrayList((Creator<RoomInfo>) arrayListRooms);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringList(location);
dest.writeStringList(time);
dest.writeString(sTime);
dest.writeString(sLoc);
dest.writeString(sTitle);
dest.writeString(sFirstRoom);
dest.writeString(sSecondRoom);
dest.writeStringList(aTitle);
dest.writeParcelable(intent, flags);
dest.writeIntArray(hour);
dest.writeIntArray(min);
dest.writeList(arrayListRooms);
}
我还已经拥有所有宗地变量的getter和setter方法。
我该怎么办? 预先感谢。
答案 0 :(得分:0)
在Parcelable
中使用自定义PendingIntent
对象时,经常会遇到这样的问题。本质上,系统将取消包裹并重新包裹您的extras
捆绑包,但是由于它无权访问您的类或类加载器,因此数据将丢失。
本文提供了更多详细信息:https://commonsware.com/blog/2016/07/22/be-careful-where-you-use-custom-parcelables.html