如何发送包含双嵌套Parcelable数组的Parcelable对象

时间:2015-10-11 17:05:46

标签: java android arrays android-asynctask parcelable

我正在尝试将我的启动活动中的对象写入Parcel,然后使用intent将其发送到我的Main活动。但是,在我的Main活动中检索ParacelableArrayExtra时,我得到一个空指针异常。所以我检查null以处理null条件。但是,预测对象在放入intent之前确实有数据,因此不应满足此条件。

我好像对象数组永远不会被置于意图之内。我这是因为我的数据被错误地分配了?

StartUpActivity.java

public class StartUpActivity extends AppCompatActivity {

    public static final String FORECAST_KEY = "FORECAST_KEY";
    private Forecast[] mForecasts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new BackgroundTask(this).execute();
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
    }


    private class BackgroundTask extends AsyncTask {
        private Intent mIntent;
        private Context mContext;

        private static final String TAG = "BACKGROUND_TASK";

        public BackgroundTask(Context context) {
            mContext = context;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mIntent = new Intent(StartUpActivity.this, MainActivity.class);
        }

        @Override
        protected Object doInBackground(Object[] params) {
            mForecasts = getForecasts();
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            // mForecasts is populated and does contain data
            mIntent.putExtra(StartUpActivity.FORECAST_KEY, mForecasts);

            // mForecasts in not located in mIntent
            startActivity(mIntent);
            finish();
        }

        // Other methods omitted
    }
}

MainActivity.java

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    setSupportActionBar(mToolbar);

    intent.getParcelableArrayExtra(StartUpActivity.FORECAST_KEY);

    Parcelable[] allForecastParcelables = getIntent().getParcelableArrayExtra(StartUpActivity.FORECAST_KEY);
    if (allForecastParcelables != null) {
        mForecasts = new Forecast[allForecastParcelables.length];
        for (int i = 0 ; i < allForecastParcelables.length; i++) {
            mForecasts[i] = (Forecast) allForecastParcelables[i];
        }
    }
    else {
        mForecasts = null;
    }
    setupSlidingTabs();
}

以下是我的Model对象,它实现了Parcelable

Forecast.java

public class Forecast implements Parcelable{

Day[] mDays;

public Day[] getDailyForecast() {
    return mDays;
}

public void setDailyForecast(Day[] days) {
    mDays = days;
}


@Override
public int describeContents() {
    return 0;
}

public Forecast() {}

private Forecast(Parcel in) {
   in.readTypedArray(mDays, Day.CREATOR);
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeTypedArray(mDays, flags);

}
public static final Creator<Forecast> CREATOR = new Creator<Forecast>() {
    @Override
    public Forecast createFromParcel(Parcel source) {
        return new Forecast(source);
    }

    @Override
    public Forecast[] newArray(int size) {
        return new Forecast[size];
    }
};
}

Day.java

公共课日实施Parcelable {

private Hour[] mHours;
private Average mAverage;

public Hour[] getHourlyForecast() {
    return mHours;
}

public void setHourlyForecast(Hour[] hours) {
    mHours = hours;
}

public Average getAverageForecast() {
    return mAverage;
}

public void setAverageForecast(Average average) {
    mAverage = average;
}

@Override
public int describeContents() {
    return 0;
}
public Day() {}

private Day(Parcel in) {
    in.readTypedArray(mHours, Hour.CREATOR);
    mAverage = in.readParcelable(getClass().getClassLoader());
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeTypedArray(mHours, flags);
    dest.writeParcelable(mAverage, flags);

}
public static final Creator<Day> CREATOR = new Creator<Day>() {
    @Override
    public Day createFromParcel(Parcel source) {
        return new Day(source);
    }

    @Override
    public Day[] newArray(int size) {
        return new Day[size];
    }
};
}

Average.java     public class Average实现Parcelable {

private String mWindCompassDireciton;

public String getWindCompassDireciton() {
    return mWindCompassDireciton;
}

public void setWindCompassDireciton(String windCompassDireciton) {
    mWindCompassDireciton = windCompassDireciton;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(mWindCompassDireciton);
}

public Average() {}

private Average(Parcel in) {
    mWindCompassDireciton = in.readString();

}

public static final Creator<Average> CREATOR = new Creator<Average>() {
    @Override
    public Average createFromParcel(Parcel source) {
        return new Average(source);
    }

    @Override
    public Average[] newArray(int size) {
        return new Average[size];
    }
};
}

小时与average.java相似

我有什么可以忽略的吗?

1 个答案:

答案 0 :(得分:0)

parcelable对象中的每个ArrayList和对象也应该是和平的,请检查example。但我建议您发送GSON并将其转换为String并将其作为字符串在活动之间发送并再次转换为您的对象,请检查example