Parcelable 在写入可序列化对象时遇到 IOException ......?

Posted

技术标签:

【中文标题】Parcelable 在写入可序列化对象时遇到 IOException ......?【英文标题】:Parcelable encountered IOException writing serializable object.......................? 【发布时间】:2020-07-22 15:03:38 【问题描述】:

我在尝试将一个 Activity 的对象引用传递给另一个时遇到问题,我认为罪魁祸首是我作为该对象上的一个字段的列表。我认为这是因为当我将列表更改为瞬态时,对象似乎能够通过,但这当然不能解决我的问题,因为现在列表在新 Activity 中为空。

源活动代码

    package com.example.animequiz.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.animequiz.R;
import com.example.animequiz.dao.AnimeDAO;
import com.example.animequiz.model.Anime;
import com.example.animequiz.ui.adapter.AnimeListAdapter;

import java.util.List;

import static com.example.animequiz.ui.activity.HomePageActivityConstants.KEY_ANIME;

public class AnimesActivity extends AppCompatActivity 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anime);
        configureAnimeList();
    

    private void configureAnimeList() 
        ListView animeList = findViewById(R.id.activity_anime_listview);
        final List<Anime> animes = new AnimeDAO().list();
        animeList.setAdapter(new AnimeListAdapter(animes, this));
        animeList.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) 
                Anime selectedAnime = animes.get(position);
                goToLevels(selectedAnime);
            
        );
    

    private void goToLevels(Anime selectedAnime) 
        Intent intent = new Intent(AnimesActivity.this, LevelsActivity.class);
        intent.putExtra(KEY_ANIME, selectedAnime);
        startActivity(intent);
    

动漫类代码

    package com.example.animequiz.model;

import java.io.Serializable;
import java.util.List;

public class Anime implements Serializable 

    private final String title;
    private final String image;
    private final List<Level> levelList;

    public Anime(String title, String image, List<Level> levelList) 
        this.title = title;
        this.image = image;
        this.levelList = levelList;
    

    public String getTitle() 
        return title;
    

    public String getImage() 
        return image;
    

    public double getAnimeCompletionPercentage() 
        double animeCompletionPercentage = 0;
        for (Level level: levelList) 
            animeCompletionPercentage += level.getLevelCompletionPercentage();
        
        animeCompletionPercentage = animeCompletionPercentage/levelList.size();
        return animeCompletionPercentage;
    

    public List<Level> getLevelList() 
        return levelList;
    


关卡代码

package com.example.animequiz.model;
import java.io.Serializable;
import java.util.List;

public class Level implements Serializable 

    private final List<AnimeCharacter> animeCharacterList;

    public Level(List<AnimeCharacter> animeCharacterList) 
        this.animeCharacterList = animeCharacterList;
    

    public double getLevelCompletionPercentage() 
        double levelCompletionPercentage = 0;
        for (AnimeCharacter animeCharacter: animeCharacterList) 
            if (animeCharacter.isKnownByPlayer() == true)
                levelCompletionPercentage++;
        
        levelCompletionPercentage = levelCompletionPercentage/animeCharacterList.size();
        return levelCompletionPercentage;
    

    public List<AnimeCharacter> getAnimeCharacterList() 
        return animeCharacterList;
    


Logcat

04-09 17:29:28.296 13995-13995/com.example.animequiz E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.animequiz, PID: 13995
    java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.animequiz.model.Anime)
        at android.os.Parcel.writeSerializable(Parcel.java:1394)
        at android.os.Parcel.writeValue(Parcel.java:1341)
        at android.os.Parcel.writeArrayMapInternal(Parcel.java:644)
        at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
        at android.os.Bundle.writeToParcel(Bundle.java:1034)
        at android.os.Parcel.writeBundle(Parcel.java:669)
        at android.content.Intent.writeToParcel(Intent.java:7485)
        at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2411)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1496)
        at android.app.Activity.startActivityForResult(Activity.java:3745)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
        at android.app.Activity.startActivityForResult(Activity.java:3706)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
        at android.app.Activity.startActivity(Activity.java:4016)
        at android.app.Activity.startActivity(Activity.java:3984)
        at com.example.animequiz.ui.activity.AnimesActivity.goToLevels(AnimesActivity.java:46)
        at com.example.animequiz.ui.activity.AnimesActivity.access$000(AnimesActivity.java:21)
        at com.example.animequiz.ui.activity.AnimesActivity$1.onItemClick(AnimesActivity.java:38)
        at android.widget.AdapterView.performItemClick(AdapterView.java:305)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
        at android.widget.AbsListView.onTouchUp(AbsListView.java:3872)
        at android.widget.AbsListView.onTouchEvent(AbsListView.java:3637)
        at android.view.View.dispatchTouchEvent(View.java:8471)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2399)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2092)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2369)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1719)
        at android.app.Activity.dispatchTouchEvent(Activity.java:2742)
        at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2330)
        at android.view.View.dispatchPointerEvent(View.java:8666)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4123)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3989)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
        at android.view

【问题讨论】:

你能在 Level 类中发布什么吗?如果您希望Anime 类可序列化,则其所有属性也应该是可序列化类的实例。这意味着Level 类也应该是可序列化的 放下这个可序列化的 mambo jambo。使用 Gson。非常灵活,非常强大。使用 Gson.toJson,将字符串传递到另一端。比使用 Gson.fromJson 重建对象。 @RezoShalikashvili 我刚刚编辑它以包含 Level.class 的代码 @gmfiga 所以 Java 序列化就像一棵树一样工作。它从您的案例“Anime”中的根类开始。该类的每个属性都应该是可序列化的。它在属性中递归。因此,在您的情况下,“Level”类具有“AnimeCharacter”类型的属性。这意味着“AnimeCharacter”也应该是可序列化的。等等。直到您达到字符串和数字等促销类型。同样重要的是,可序列化类应该具有零参数构造函数。如果你愿意,我会用例子写完整的答案。您可以忽略这一点,也可以使用 gson。但请理解这将很有用。 @RezoShalikashvili 你是对的。我完全忘记了在我的 AnimeCharacter.class 上实现 Serializable。现在效果很好。谢谢! 【参考方案1】:

因此,Java 序列化就像一棵树一样工作。在您的情况下,它从根类开始Anime。该类的每个属性都应该是可序列化的。它在属性中递归。因此,在您的情况下,“Level”类具有“AnimeCharacter”类型的属性。这意味着“AnimeCharacter”也应该是可序列化的。等等。直到您达到字符串和数字等促销类型。同样重要的是,可序列化类应该具有零参数构造函数。您也可以忽略这一点并使用“Gson”。但是理解这一点在未来会很有用,因为所有序列化引擎都以类似的方式工作。

【讨论】:

以上是关于Parcelable 在写入可序列化对象时遇到 IOException ......?的主要内容,如果未能解决你的问题,请参考以下文章

Parcelable 在写入可序列化对象时遇到 IOException ......?

获取 Parcelable 遇到 IOException 写入可序列化对象

无法解决“Parcelable 遇到写入可序列化对象的 IOException”

Parcelable 遇到 IOException 写入可序列化对象 getactivity()

java.lang.RuntimeException: Parcelable 遇到 IOException 写入可序列化对象 (name = io.realm.StationAndLocationMo

java.lang.RuntimeException: Parcelable 遇到 IOException 写入可序列化