0

在 Android 中将日期值写入 Parceable 类时,我们遇到了空指针异常。

我们想知道将日期值写入 Parceable 类的正确方法是什么

这是 Parceable 类的代码


import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;

import com.pregnancycompanionapp.companion.R;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * DAO for the expecting mother.
 * 
 * @author Magnus Dahl
 */
public class Mommy implements Parcelable, Serializable {

    private static final long serialVersionUID = 1L;
    private Date mDueDate = Calendar.getInstance().getTime();
    private int mMode = Utils.LAST_PERIOD_DAYS;
    private int mAge;
    private int mWeightPounds;
    private float mWeightKG;
    private int mHeightFeet;
    private int mHeightInches;
    private float mHeightCM;
    private int mExercise = Utils.NONE;
    private String mDoctorsNumber;
    private int mTheme = R.style.theme_purple;
    private int mMeasurement = R.string.SET_U_S__KEY;
    private float mBMI;
    private float mBMR;
    private int mID;
    private Date mCurrentDate;
    private boolean mSoundEnabled;
    // private Map<Integer, Integer> userSyncPeriods;
    // private SparseArray<Parcelable> userSyncPeriods;

    /**
     * TODO: Constant description goes in here.
     */
    public static final Parcelable.Creator<Mommy> CREATOR = new Parcelable.Creator<Mommy>() {

        /**
         * @see android.os.Parcelable.Creator#createFromParcel(android.os.Parcel)
         */
        @Override
        public Mommy createFromParcel(final Parcel in) {
            return new Mommy(in);
        }

        /**
         * @see android.os.Parcelable.Creator#newArray(int)
         */
        @Override
        public Mommy[] newArray(final int size) {
            return new Mommy[size];
        }
    };

    /**
     * TODO: Constructor description goes in here.
     */
    public Mommy() {
        //
    }

    /**
     * TODO: Constructor description goes in here.
     * 
     * @param in
     */
    // @SuppressWarnings("unchecked")
    /*Modified on 19-Apr-2013 (Start)
     * Reason for Modification: Read From Parcel should be of same order of Write To Parcel
     * */
    public Mommy(final Parcel in) {
        this.mMode = in.readInt();
        this.mDueDate = new Date(in.readLong());
        this.mAge = in.readInt();
        this.mWeightPounds = in.readInt();
        this.mHeightFeet = in.readInt();
        this.mHeightInches = in.readInt();
        this.mExercise = in.readInt();
        this.mDoctorsNumber = in.readString();
        this.mTheme = in.readInt();
        this.mMeasurement = in.readInt();
        this.mBMI = in.readFloat();
        this.mBMR = in.readFloat();
        this.mWeightKG = in.readFloat();
        this.mHeightCM = in.readFloat();
        this.mID = in.readInt();
        this.mCurrentDate = new Date(in.readLong());
        this.mSoundEnabled = (in.readByte() == 1);
        // this.userSyncPeriods =
        // in.readHashMap(HashMap.class.getClassLoader());
        // this.userSyncPeriods =
        // in.readSparseArray(SparseArray.class.getClassLoader());
    }
     /*Modified on 19-Apr-2013 (End)*/
    /**
     * @see android.os.Parcelable#describeContents()
     */
    @Override
    public int describeContents() {
        return 0;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @return String
     */
    public int getAge() {
        return this.mAge;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @return the BMI
     */
    public float getBMI() {
        return this.mBMI;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @return the BMR
     */
    public float getBMR() {
        return this.mBMR;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @return Date
     */
    public Date getCurrentDate() {
        return this.mCurrentDate;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @return String
     */
    public String getDoctorsNumber() {
        return this.mDoctorsNumber;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @return Date
     */
    public Date getDueDate() {
        return this.mDueDate;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @return int
     */
    public int getExercise() {
        return this.mExercise;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @return float
     */
    public float getHeightCM() {
        return this.mHeightCM;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @return int
     */
    public int getHeightFeet() {
        return this.mHeightFeet;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @return int
     */
    public int getHeightInches() {
        return this.mHeightInches;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @return integer
     */
    public int getID() {
        return this.mID;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @return the measurement
     */
    public int getMeasurement() {
        return this.mMeasurement;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @return the mode
     */
    public int getMode() {
        return this.mMode;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @return integer
     */
    public int getTheme() {
        return this.mTheme;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @return float
     */
    public float getWeightKG() {
        return this.mWeightKG;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @return int
     */
    public int getWeightPounds() {
        return this.mWeightPounds;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @return boolean
     */
    public boolean isSoundEnabled() {
        return this.mSoundEnabled;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @param age
     */
    public void setAge(final String age) {
        if ((age != null) && (age != "")) {
            this.mAge = Integer.parseInt(age);
        }
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @param bmi
     *            the BMI to set
     */
    public void setBMI(final float bmi) {
        this.mBMI = bmi;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @param bmr
     *            the BMR to set
     */
    public void setBMR(final float bmr) {
        this.mBMR = bmr;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @param currentDate
     */
    public void setCurrentDate(final Date currentDate) {
        this.mCurrentDate = currentDate;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @param doctorsNumber
     */
    public void setDoctorsNumber(final String doctorsNumber) {
        this.mDoctorsNumber = doctorsNumber;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @param dueDate
     */
    public void setDueDate(final Date dueDate) {
        this.mDueDate = dueDate;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @param exercise
     */
    public void setExercise(final int exercise) {
        this.mExercise = exercise;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @param heightCM
     */
    public void setHeightCM(final String heightCM) {
        if ((heightCM != null) && (heightCM != "")) {
            if(heightCM.contains(","))
            {
                String heightCMFix = heightCM.replace(",", "."); //fixes possible localization issues with commas and periods in long numbers
                this.mHeightCM = Float.parseFloat(heightCMFix);
            }
            else
                this.mHeightCM = Float.parseFloat(heightCM);
        }
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @param heightFeet
     */
    public void setHeightFeet(final String heightFeet) {
        if ((heightFeet != null) && (heightFeet != "")) {
            this.mHeightFeet = Integer.parseInt(heightFeet);
        }
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @param heightInches
     */
    public void setHeightInches(final String heightInches) {
        if ((heightInches != null) && (heightInches != "")) {
            this.mHeightInches = Integer.parseInt(heightInches);
        }
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @param id
     */
    public void setID(final int id) {
        this.mID = id;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @param measurement
     *            the measurement to set
     */
    public void setMeasurement(final int measurement) {
        this.mMeasurement = measurement;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @param mode
     *            the mode to set
     */
    public void setMode(final int mode) {
        this.mMode = mode;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @param soundEnabled
     */
    public void setSoundEnabled(final int soundEnabled) {
        if (soundEnabled == 0) {
            this.mSoundEnabled = false;
        } else {
            this.mSoundEnabled = true;
        }
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @param theme
     */
    public void setTheme(final int theme) {
        this.mTheme = theme;
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @param weightKG
     */
    public void setWeightKG(final String weightKG) {
        if ((weightKG != null) && (weightKG != "")) {
            this.mWeightKG = Float.parseFloat(weightKG);
        }
    }

    /**
     * TODO: Method description goes in here.
     * 
     * @param weightPounds
     */
    public void setWeightPounds(final String weightPounds) {
        if ((weightPounds != null) && (weightPounds != "")) {
            this.mWeightPounds = Integer.parseInt(weightPounds);
        }
    }

    // public Map<Integer, Integer> getUserSyncPeriods() {
    // return this.userSyncPeriods;
    // }

    // public void setUserSyncPeriods(Map<Integer, Integer> userSyncPeriods) {
    // this.userSyncPeriods = userSyncPeriods;
    // }

    /**
     * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
     */
    @Override
    /*Modified on 19-Apr-2013 (Start)
     * Reason for Modification: Write To Parcel should be of same order of read from Parcel
     * */
    public void writeToParcel(final Parcel dest, final int flags) {
        dest.writeInt(this.mMode);
        dest.writeLong(this.mDueDate.getTime());
        dest.writeInt(this.mAge);
        dest.writeInt(this.mWeightPounds);
        dest.writeInt(this.mHeightFeet);
        dest.writeInt(this.mHeightInches);
        dest.writeInt(this.mExercise);
        dest.writeString(this.mDoctorsNumber);
        dest.writeInt(this.mTheme);
        dest.writeInt(this.mMeasurement);
        dest.writeFloat(this.mBMI);
        dest.writeFloat(this.mBMR);
        dest.writeFloat(this.mWeightKG);
        dest.writeFloat(this.mHeightCM);
        dest.writeInt(this.mID);
        dest.writeLong(this.mCurrentDate.getTime());
        dest.writeByte((byte) (this.mSoundEnabled ? 1 : 0));
        // dest.writeMap(this.userSyncPeriods);
        // dest.writeSparseArray( (this.userSyncPeriods<Parcelable>));
    }
     /*Modified on 19-Apr-2013 (End)*/
}

这是崩溃报告的堆栈跟踪


1   com.pregnancycompanionapp.companion.Mommy.writeToParcel Mommy.java, line 473
2   android.os.Parcel.writeParcelable   Parcel.java, line 1156
3   android.os.Parcel.writeValue    Parcel.java, line 1075
4   android.os.Parcel.writeMapInternal  Parcel.java, line 493
5   android.os.Bundle.writeToParcel Bundle.java, line 1612
6   android.os.Parcel.writeBundle   Parcel.java, line 507
7   android.support.v4.app.FragmentState.writeToParcel  Fragment.java, line 132
8   android.os.Parcel.writeTypedArray   Parcel.java, line 1004
9   android.support.v4.app.FragmentManagerState.writeToParcel   FragmentManager.java, line 357
10  android.os.Parcel.writeParcelable   Parcel.java, line 1156
11  android.os.Parcel.writeValue    Parcel.java, line 1075
12  android.os.Parcel.writeMapInternal  Parcel.java, line 493
13  android.os.Bundle.writeToParcel Bundle.java, line 1612
14  android.os.Parcel.writeBundle   Parcel.java, line 507
15  android.app.ActivityManagerProxy.activityStopped    ActivityManagerNative.java, line 2002
16  android.app.ActivityThread.handleStopActivity   ActivityThread.java, line 2878
17  android.app.ActivityThread.access$900   ActivityThread.java, line 127
18  android.app.ActivityThread$H.handleMessage  ActivityThread.java, line 1176
19  android.os.Handler.dispatchMessage  Handler.java, line 99
20  android.os.Looper.loop  Looper.java, line 137
21  android.app.ActivityThread.main ActivityThread.java, line 4507
22  java.lang.reflect.Method.invokeNative   
23  java.lang.reflect.Method.invoke Method.java, line 511
24  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run  ZygoteInit.java, line 790
25  com.android.internal.os.ZygoteInit.main ZygoteInit.java, line 557
26  dalvik.system.NativeStart.main

4

1 回答 1

2
 private Date mDueDate = Calendar.getInstance().getTime();

而是序列化 Date 对象,获取以毫秒为单位的长表示时间并序列化该长。当您反序列化对象时,您可以重新创建 Date 对象

于 2013-05-22T08:21:52.893 回答