gpt4 book ai didi

android - 尝试在自定义对象数组上使用 ParcelableArrayList 时发生 ClassCastException

转载 作者:行者123 更新时间:2023-11-29 17:50:33 25 4
gpt4 key购买 nike

我正在尝试实现 Parcelable而不是 Serializable因为它应该更有效率。

来 self 的 MainActivity我想传递通用类型的对象 ArrayList<LogChartData>到相同的 Activity ,以便我在方向改变时得到它。

如果我克服了这个错误,我肯定会遇到更多错误,但现在当我尝试运行我的应用程序时,我得到了,

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;
}
}

最佳答案

您不能像这样直接将字符串变量直接转换为 Date 对象

(Date)in.readValue(LogChartData.class.getClassLoader());

需要使用SimpleDateFormat,从指定的DateFormat中解析出String,然后存储到Date对象中

假设你的日期格式为"yyyy-MM-dd"

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

this.chartDate= format.parse(in.readValue(LogChartData.class.getClassLoader()));

关于android - 尝试在自定义对象数组上使用 ParcelableArrayList 时发生 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23149290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com