抽象类序列化异常
我正在尝试开发一个小 Android 游戏,但我很难理解如何克服这个问题。
我想做的是在活动之间发送数据。为了实现这一点,我正在使用意图附加功能。 我尝试发送的数据是 ArrayList
Item.java,
public abstract class Item implements Serializable {
private String fieldA;
private int fieldB;
*getters and setters*
abstract void use();
}
每个项目的使用方法都不同,这就是为什么我使用抽象
(例如创建项目)
Item hpPotion = new Item() {
@Override
public void use() {
player.setHealth(player.getHealth() + 10)
}
};
时收到错误:
Intent intent = new Intent(HistoryActivity.this,BagActivity.class);
intent.putExtra("items",player.getItems());
someActivityResultLauncher.launch(intent,options);
,然后在启动新的 Activity StackTrace
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.decisions, PID: 22018
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.decisions.HistoryActivity$7)
at android.os.Parcel.writeSerializable(Parcel.java:2116)
at android.os.Parcel.writeValue(Parcel.java:1886)
at android.os.Parcel.writeList(Parcel.java:1095)
at android.os.Parcel.writeValue(Parcel.java:1835)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:978)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1629)
at android.os.Bundle.writeToParcel(Bundle.java:1303)
at android.os.Parcel.writeBundle(Parcel.java:1047)
at android.content.Intent.writeToParcel(Intent.java:10871)
at android.app.IActivityTaskManager$Stub$Proxy.startActivity(IActivityTaskManager.java:3887)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1727)
at android.app.Activity.startActivityForResult(Activity.java:5405)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:574)
at androidx.core.app.ActivityCompat.startActivityForResult(ActivityCompat.java:237)
at androidx.activity.ComponentActivity$2.onLaunch(ComponentActivity.java:208)
at androidx.activity.result.ActivityResultRegistry$2.launch(ActivityResultRegistry.java:166)
at com.example.decisions.HistoryActivity$2.onClick(HistoryActivity.java:96)
at android.view.View.performClick(View.java:7559)
at android.view.View.performClickInternal(View.java:7536)
at android.view.View.access$3600(View.java:828)
at android.view.View$PerformClick.run(View.java:28700)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:7861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:600)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
Caused by: java.io.NotSerializableException: com.example.decisions.HistoryActivity
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1240)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1604)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1565)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1488)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1234)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:354)
at android.os.Parcel.writeSerializable(Parcel.java:2111)
at android.os.Parcel.writeValue(Parcel.java:1886)
at android.os.Parcel.writeList(Parcel.java:1095)
at android.os.Parcel.writeValue(Parcel.java:1835)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:978)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1629)
at android.os.Bundle.writeToParcel(Bundle.java:1303)
at android.os.Parcel.writeBundle(Parcel.java:1047)
at android.content.Intent.writeToParcel(Intent.java:10871)
at android.app.IActivityTaskManager$Stub$Proxy.startActivity(IActivityTaskManager.java:3887)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1727)
at android.app.Activity.startActivityForResult(Activity.java:5405)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:574)
at androidx.core.app.ActivityCompat.startActivityForResult(ActivityCompat.java:237)
at androidx.activity.ComponentActivity$2.onLaunch(ComponentActivity.java:208)
at androidx.activity.result.ActivityResultRegistry$2.launch(ActivityResultRegistry.java:166)
at com.example.decisions.HistoryActivity$2.onClick(HistoryActivity.java:96)
at android.view.View.performClick(View.java:7559)
at android.view.View.performClickInternal(View.java:7536)
at android.view.View.access$3600(View.java:828)
at android.view.View$PerformClick.run(View.java:28700)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
```
What would be the best approach to solve my problem?
Thank you all
Im trying to develop a little android game and i'm having a hard time understanding how can i surpass this problem.
What i'm trying to do is send data between activities.To achieve that i'm using the intent extras.
The data that im trying to send is an ArrayList<Item
Item.java
public abstract class Item implements Serializable {
private String fieldA;
private int fieldB;
*getters and setters*
abstract void use();
}
the use method will be different on each item so thats why im using abstract
e.g creation item
Item hpPotion = new Item() {
@Override
public void use() {
player.setHealth(player.getHealth() + 10)
}
};
and then im getting an error when starting the new Activity
Intent intent = new Intent(HistoryActivity.this,BagActivity.class);
intent.putExtra("items",player.getItems());
someActivityResultLauncher.launch(intent,options);
StackTrace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.decisions, PID: 22018
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.decisions.HistoryActivity$7)
at android.os.Parcel.writeSerializable(Parcel.java:2116)
at android.os.Parcel.writeValue(Parcel.java:1886)
at android.os.Parcel.writeList(Parcel.java:1095)
at android.os.Parcel.writeValue(Parcel.java:1835)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:978)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1629)
at android.os.Bundle.writeToParcel(Bundle.java:1303)
at android.os.Parcel.writeBundle(Parcel.java:1047)
at android.content.Intent.writeToParcel(Intent.java:10871)
at android.app.IActivityTaskManager$Stub$Proxy.startActivity(IActivityTaskManager.java:3887)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1727)
at android.app.Activity.startActivityForResult(Activity.java:5405)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:574)
at androidx.core.app.ActivityCompat.startActivityForResult(ActivityCompat.java:237)
at androidx.activity.ComponentActivity$2.onLaunch(ComponentActivity.java:208)
at androidx.activity.result.ActivityResultRegistry$2.launch(ActivityResultRegistry.java:166)
at com.example.decisions.HistoryActivity$2.onClick(HistoryActivity.java:96)
at android.view.View.performClick(View.java:7559)
at android.view.View.performClickInternal(View.java:7536)
at android.view.View.access$3600(View.java:828)
at android.view.View$PerformClick.run(View.java:28700)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:7861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:600)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
Caused by: java.io.NotSerializableException: com.example.decisions.HistoryActivity
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1240)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1604)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1565)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1488)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1234)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:354)
at android.os.Parcel.writeSerializable(Parcel.java:2111)
at android.os.Parcel.writeValue(Parcel.java:1886)
at android.os.Parcel.writeList(Parcel.java:1095)
at android.os.Parcel.writeValue(Parcel.java:1835)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:978)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1629)
at android.os.Bundle.writeToParcel(Bundle.java:1303)
at android.os.Parcel.writeBundle(Parcel.java:1047)
at android.content.Intent.writeToParcel(Intent.java:10871)
at android.app.IActivityTaskManager$Stub$Proxy.startActivity(IActivityTaskManager.java:3887)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1727)
at android.app.Activity.startActivityForResult(Activity.java:5405)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:574)
at androidx.core.app.ActivityCompat.startActivityForResult(ActivityCompat.java:237)
at androidx.activity.ComponentActivity$2.onLaunch(ComponentActivity.java:208)
at androidx.activity.result.ActivityResultRegistry$2.launch(ActivityResultRegistry.java:166)
at com.example.decisions.HistoryActivity$2.onClick(HistoryActivity.java:96)
at android.view.View.performClick(View.java:7559)
at android.view.View.performClickInternal(View.java:7536)
at android.view.View.access$3600(View.java:828)
at android.view.View$PerformClick.run(View.java:28700)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
```
What would be the best approach to solve my problem?
Thank you all
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试直接或间接序列化
Activity
(您尝试序列化的内容引用了Activity
)。这是不可能的,因为Activity
没有实现 Serialized。要正确实现 Serialized,所有成员变量都必须是原语或本身实现 Serialized。编辑:如果
Item
是Activity
的子类,它可能会获取隐式父引用。在这种情况下,请将其设为静态类。You're trying to serialize an
Activity
, either directly or indirectly (something you're trying to Serialize has a reference to anActivity
). That's not possible, becauseActivity
does not implement Serializable. To properly implement Serializable, all of your member variables must either be primitives or implement Serializable themselves.Edit: If
Item
is a subclass ofActivity
, it may be picking up the implicit parent reference. In that case, make it a static class.