2016-04-08 80 views
0

我創建了包含布爾數組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]; 
    } 
}; 

被寫之前,但在另一端陣列的心不是空給了我一個空數組錯誤,當我嘗試做無論是

in.readBooleanArray(days); 
days = createBooleanArray(); 

我認爲我也跟着創建parcelable對象正確的方法,大部分值似乎是罰款(還沒有檢查所有)

這裏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

您可以發佈logcat嗎? –

+0

@ TungD.Nguyen我加了logcat – user3074140

回答

1

是的,它是null。看看你的readwrite

// 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個值 - >當然是空。因此你應該改變寫入位置或讀取位置。

+0

是的,現在工作。我不知道訂購的重要性。謝謝 – user3074140

1

我認爲你還沒有啓動你的數組。將this()添加到第二個構造函數

protected Alarm(Parcel in) { 
    this();//add this to make sure arrays are initialised 
    dest.writeInt(hours); 
    dest.writeInt(minutes); 
+0

我現在得到一個錯誤的數組長度錯誤 – user3074140

+0

你需要按照你寫的東西的順序從包中讀取。我看到'ID'和'Day'交換 –