gpt4 book ai didi

Java android java.lang.RuntimeException : Parcelable encountered IOException writing serializable object

转载 作者:搜寻专家 更新时间:2023-11-01 07:46:29 46 4
gpt4 key购买 nike

我想在完成 Activity 的结果中放置一个对象列表我创建了一个类,如下所示:

 public class Vals implements Serializable {
public ArrayList<RowBean> data;

public Vals(ArrayList<RowBean> data) {
this.data = data;
}
}

下一步 Activity ( fragment )我在完成 Activity 后执行了此操作:

private void finishWithResult() {
Bundle conData = new Bundle();
conData.putString("param_result", counter + "");
Intent intent = new Intent();
intent.putExtras(conData);
ArrayList<RowBean> rows = new ArrayList<>();
for(RowBean row : rowBeen){
if(row.isSelected())
rows.add(row);
}
intent.putExtra(ROW_BEAN_DATA, new Vals(rows));
getActivity().setResult(RESULT_OK, intent);
getActivity().finish();
}

在控制台中我有:

FATAL EXCEPTION: main
Process: com.maps, PID: 10222 java.lang.RuntimeException:
Parcelable encountered IOException writing serializable object (name = com.maps.Utils.Vals) at
android.os.Parcel.writeSerializable(Parcel.java:1316) at android.os.Parcel.writeValue(Parcel.java:1264) at android.os.Parcel.writeArrayMapInternal(Parcel.java:618) at android.os.Bundle.writeToParcel(Bundle.java:1692) at android.os.Parcel.writeBundle(Parcel.java:636) at android.content.Intent.writeToParcel(Intent.java:7582) at android.app.ActivityManagerProxy.finishActivity(ActivityManagerNative.java:2517)at android.app.Activity.finish(Activity.java:4324) at com.maps.Fragment.FragmentListOfAllObjetsToReportActivity.finishWithResult(FragmentListOfAllObjetsToReportActivity.java:156) at com.maps.Fragment.FragmentListOfAllObjetsToReportActivity.confirmChoose(FragmentListOfAllObjetsToReportActivity.java:160) at com.maps.Fragment.FragmentListOfAllObjetsToReportActivity.access$200(FragmentListOfAllObjetsToReportActivity.java:34) at com.maps.Fragment.FragmentListOfAllObjetsToReportActivity$3.onClick(FragmentListOfAllObjetsToReportActivity.java:102) at android.view.View.performClick(View.java:4640)

这是 onActivityResult :

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 90:
if (resultCode == RESULT_OK) {
Bundle res = data.getExtras();
String result = res.getString("param_result");
int count = Integer.parseInt(result);
if (count != 1)
tvChooseObjects.setText("Wybrano " + count + " obiekty");
else
tvChooseObjects.setText("Wybrano " + count + " obiekt");
rowBeen = ((Vals) getIntent().getSerializableExtra(FragmentListOfAllObjetsToReportActivity.ROW_BEAN_DATA)).data;

Toast.makeText(getApplicationContext(), rowBeen.size() + " " , Toast.LENGTH_LONG).show();

}
break;
}

最佳答案

尝试在 ValsRawBean 类中用 Parcelable 替换 Serializable 实现

将 *onActivityResult** 中的 getSerializableExtra 替换为 getParcelableExtra

Vals.java

 public class Vals implements Serializable, Parcelable {
public ArrayList<RowBean> data;

public Vals(ArrayList<RowBean> data) {
this.data = data;
}

protected Vals(Parcel in) {
if (in.readByte() == 0x01) {
data = new ArrayList<RowBean>();
in.readList(data, RowBean.class.getClassLoader());
} else {
data = null;
}
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
if (data == null) {
dest.writeByte((byte) (0x00));
} else {
dest.writeByte((byte) (0x01));
dest.writeList(data);
}
}

@SuppressWarnings("unused")
public static final Parcelable.Creator<Vals> CREATOR = new Parcelable.Creator<Vals>() {
@Override
public Vals createFromParcel(Parcel in) {
return new Vals(in);
}

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

RawBean.java

public class RowBean implements Serializable, Parcelable {

public String title;
public boolean selected;

public RowBean(){
}

public boolean isSelected() {
return selected;
}

public void setSelected(boolean selected) {
this.selected = selected;
}

public RowBean(boolean selected, String title) {
this.selected = selected;
this.title = title;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

protected RowBean(Parcel in) {
title = in.readString();
selected = in.readByte() != 0x00;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
dest.writeByte((byte) (selected ? 0x01 : 0x00));
}

@SuppressWarnings("unused")
public static final Parcelable.Creator<RowBean> CREATOR = new Parcelable.Creator<RowBean>() {
@Override
public RowBean createFromParcel(Parcel in) {
return new RowBean(in);
}

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

关于Java android java.lang.RuntimeException : Parcelable encountered IOException writing serializable object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43228362/

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