I’m trying to implement Parcelable
instead of Serializable
as it’s supposed to be more efficient.
From my MainActivity
I want to pass the object of generic type ArrayList<LogChartData>
to the same activity, so that I get it on orientation change.
If I get past this one error I’m sure I’ll run in to a lot more of them, but right now when I try to run my app I’m getting,
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date
at com.testapp.util.LogChartData.<init>(LogChartData.java:60)
at com.testapp.util.LogChartData.<init>(LogChartData.java:59)
at com.testapp.util.LogChartData$1.createFromParcel(LogChartData.java:51)
at com.testapp.util.LogChartData$1.createFromParcel(LogChartData.java:1)
at android.os.Parcel.readParcelable(Parcel.java:2103)
at android.os.Parcel.readValue(Parcel.java:1965)
MainActivity.java--
ArrayList<LogChartData> logss = new ArrayList<LogChartData>();
@Override
public void onSaveInstanceState(final Bundle outState) {
outState.putParcelableArrayList("logss", logss);
outState.putParcelableArrayList("alert", alert);
outState.putBoolean("isRotate", true);
super.onSaveInstanceState(outState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
if (savedInstanceState != null) {
try
{
logss = savedInstanceState.getParcelableArrayList("logss");
alert = savedInstanceState.getParcelableArrayList("alert");
isRotate = savedInstanceState.getBoolean("isRotate");
}
catch(ClassCastException e)
{
e.printStackTrace();
}
if(!isRotate)
someFunctionCall();
// More code here..
}
}
LogCharData.java --
public class LogChartData implements Parcelable {
public Date chartDate = null;
public ArrayList<LogEntries> logs = new ArrayList<LogEntries>();
public LogChartData(Date chartDate, ArrayList<LogEntries> logs) {
super();
this.chartDate = chartDate;
this.logs = logs;
}
public LogChartData() {
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
public static final Parcelable.Creator<LogChartData> CREATOR =
new Parcelable.Creator<LogChartData>() {
public LogChartData createFromParcel(Parcel in) {
return new LogChartData(in);
}
public LogChartData[] newArray(int size) {
return new LogChartData[size];
}
};
private LogChartData(Parcel in) {
this.chartDate =
(Date)in.readValue(LogChartData.class.getClassLoader()); //Error Here..
in.readList(logs, LogChartData.class.getClassLoader());
}
public Date getChartDate() {
return chartDate;
}
public void setChartDate(Date chartDate) {
this.chartDate = chartDate;
}
public ArrayList<LogEntries> getLogs() {
return logs;
}
public void setLogs(ArrayList<LogEntries> logs) {
this.logs = logs;
}
}