抽象类序列化异常

发布于 2025-01-10 12:19:06 字数 5607 浏览 0 评论 0原文

我正在尝试开发一个小 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

本王不退位尔等都是臣 2025-01-17 12:19:06

您正在尝试直接或间接序列化 Activity(您尝试序列化的内容引用了 Activity)。这是不可能的,因为 Activity 没有实现 Serialized。要正确实现 Serialized,所有成员变量都必须是原语或本身实现 Serialized。

编辑:如果 ItemActivity 的子类,它可能会获取隐式父引用。在这种情况下,请将其设为静态类。

You're trying to serialize an Activity, either directly or indirectly (something you're trying to Serialize has a reference to an Activity). That's not possible, because Activity 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 of Activity, it may be picking up the implicit parent reference. In that case, make it a static class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文