我创建了一个包含布尔数组
的parcelable对象public class Alarm implements Parcelable {
private int hours;
private int minutes;
private int alarmTime;
private int ID;
private boolean alarmSet;
private boolean[] days;
private String[] symbols;
private String sound;
private boolean snooze;
private String picture;
public Alarm() {
days = new boolean[7];
symbols = new String[]{"月","火","水","木","金","土","日","毎日"};
}
protected Alarm(Parcel in) {
hours = in.readInt();
minutes = in.readInt();
alarmTime = in.readInt();
in.readBooleanArray(days);
ID = in.readInt();
alarmSet = in.readByte() != 0x00;
sound = in.readString();
snooze = in.readByte() != 0x00;
symbols = new String[]{"月","火","水","木","金","土","日","毎日"};
picture = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(hours);
dest.writeInt(minutes);
dest.writeInt(alarmTime);
dest.writeInt(ID);
dest.writeBooleanArray(days);
dest.writeByte((byte) (alarmSet ? 0x01 : 0x00));
dest.writeString(sound);
dest.writeByte((byte) (snooze ? 0x01 : 0x00));
dest.writeString(picture);
}
public static final Parcelable.Creator<Alarm> CREATOR = new Parcelable.Creator<Alarm>() {
@Override
public Alarm createFromParcel(Parcel in) {
return new Alarm(in);
}
@Override
public Alarm[] newArray(int size) {
return new Alarm[size];
}
};
数组在写入之前不是null,但是当我尝试执行任何操作时,另一端给出了一个空数组错误
in.readBooleanArray(days);
days = createBooleanArray();
我认为我遵循了创建可分割对象的正确方法,大多数值似乎都很好(没有检查所有这些)
这是logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.test.alarm/com.android.test.alarm.AlarmSettings}: java.lang.NullPointerException: Attempt to get length of null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at android.os.Parcel.readBooleanArray(Parcel.java:807)
at com.android.test.alarm.Alarm.<init>(Alarm.java:149)
at com.android.test.alarm.Alarm$1.createFromParcel(Alarm.java:181)
at com.android.test.alarm.Alarm$1.createFromParcel(Alarm.java:178)
at android.os.Parcel.readParcelable(Parcel.java:2252)
at android.os.Parcel.readValue(Parcel.java:2152)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2485)
at android.os.BaseBundle.unparcel(BaseBundle.java:221)
at android.os.Bundle.getParcelable(Bundle.java:755)
at com.android.test.alarm.AlarmSettings.onCreate(AlarmSettings.java:19)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
答案 0 :(得分:1)
是的null
。查看您的read
,write
:
// your Alarm(Parcel in)
hours = in.readInt();
minutes = in.readInt();
alarmTime = in.readInt();
in.readBooleanArray(days);
// Your write function:
dest.writeInt(hours);
dest.writeInt(minutes);
dest.writeInt(alarmTime);
dest.writeInt(ID);
dest.writeBooleanArray(days);
如上所述,您可以将BooleanArrays
写入第5个值,但是将其读作第4个值 - &gt;当然是空的。因此,您应该更改写入位置或读取位置。
答案 1 :(得分:1)
我认为你还没有启动你的阵列。将this()
添加到第二个构造函数
protected Alarm(Parcel in) {
this();//add this to make sure arrays are initialised
dest.writeInt(hours);
dest.writeInt(minutes);