gpt4 book ai didi

java - Parcelable 在 Intent putExtra 上写入可序列化对象时遇到 IOException

转载 作者:太空宇宙 更新时间:2023-11-04 11:56:11 25 4
gpt4 key购买 nike

我正在构建一个应用程序,我必须在其中传递 List<Object>通过Intent

我的代码:

public class ObjectModelWrapper implements Serializable{

private List<Object> objectModel;

public ObjectModelWrapper(List<Object> objectModel) {
this.objectModel = objectModel;
}

public List<Object> getObjectModel() {
return objectModel;
}
}

这是我尝试传递的数据 Intent :

holder.categoryAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstSix = new ArrayList<>();
newList = new ArrayList<>();
if (items.get(position).getGames().size() > 12) {
firstSix.addAll(items.get(position).getGames().subList(0, 6));
newList.add(firstSix);
newList.add(items.get(position).getBannerImage());
newList.add(items.get(position).getGames().subList(6, items.get(position).getGames().size()));
}else if (items.get(position).getGames().size() > 8 && items.get(position).getGames().size() < 12) {
firstSix.addAll(items.get(position).getGames().subList(0, 6));
newList.add(firstSix);
newList.add(items.get(position).getBannerImage());
newList.add(items.get(position).getGames().subList(6, items.get(position).getGames().size()));
}else if (items.get(position).getGames().size() > 3 && items.get(position).getGames().size() < 8) {
firstSix.addAll(items.get(position).getGames().subList(0, 3));
newList.add(firstSix);
newList.add(items.get(position).getBannerImage());
newList.add(items.get(position).getGames().subList(3, items.get(position).getGames().size()));
}else {
firstSix.addAll(items.get(position).getGames().subList(0, 1));
newList.add(firstSix);
newList.add(items.get(position).getBannerImage());
newList.add(items.get(position).getGames().subList(1, items.get(position).getGames().size()));
}
ObjectModelWrapper wrapper = new ObjectModelWrapper(newList);

for (int i=0; i< firstSix.size(); i++){
Log.e("GAME NAME FIRST SIX", firstSix.get(i).getName());
}

for (int i=0; i< newList.size(); i++){
Log.e("GAME NAME NEW LIST", newList.get(i).toString());
}
Intent intent = new Intent(context, CategoryInfo.class);
intent.putExtra("categoryColor", items.get(position).getCategoryLineColor());
intent.putExtra("categoryBanner", items.get(position).getBannerImage());
intent.putExtra("categoryName", items.get(position).getCategoryName());
intent.putExtra("customList", wrapper);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});

新列表是:

private transient List<Object> newList;

我知道这可能是因为 Object尚未实现Serializable 。我该如何解决这个问题?

logcat 输出:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.realtimegaming.network.models.ObjectModelWrapper)
at android.os.Parcel.writeSerializable(Parcel.java:1527)
at android.os.Parcel.writeValue(Parcel.java:1475)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:724)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408)
at android.os.Bundle.writeToParcel(Bundle.java:1157)
at android.os.Parcel.writeBundle(Parcel.java:764)
at android.content.Intent.writeToParcel(Intent.java:8687)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3082)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
at android.app.Activity.startActivityForResult(Activity.java:4225)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4522)
at android.app.Activity.startActivity(Activity.java:4490)
at adapters.LobbyAdapter$1.onClick(LobbyAdapter.java:114)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.io.NotSerializableException: java.util.ArrayList$SubList
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1224)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at java.util.ArrayList.writeObject(ArrayList.java:734)
at java.lang.reflect.Method.invoke(Native Method)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:977)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1536)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1472)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1218)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1584)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1549)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1472)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1218)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at android.os.Parcel.writeSerializable(Parcel.java:1522)
at android.os.Parcel.writeValue(Parcel.java:1475) 
at android.os.Parcel.writeArrayMapInternal(Parcel.java:724) 
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408) 
at android.os.Bundle.writeToParcel(Bundle.java:1157) 
at android.os.Parcel.writeBundle(Parcel.java:764) 
at android.content.Intent.writeToParcel(Intent.java:8687) 
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3082) 
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518) 
at android.app.Activity.startActivityForResult(Activity.java:4225) 
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50) 
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79) 
at android.app.Activity.startActivityForResult(Activity.java:4183) 
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859) 
at android.app.Activity.startActivity(Activity.java:4522) 
at android.app.Activity.startActivity(Activity.java:4490) 
at adapters.LobbyAdapter$1.onClick(LobbyAdapter.java:114) 
at android.view.View.performClick(View.java:5637) 
at android.view.View$PerformClick.run(View.java:22429) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119)

最佳答案

为什么在列表中使用对象?尝试为所有列表的对象创建一个基类,并为详细信息创建继承类。然后,在每个类中,您可以实现自己的Parcelable

关于java - Parcelable 在 Intent putExtra 上写入可序列化对象时遇到 IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41376916/

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