Parcelable遇到IOException编写可序列化对象…?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Parcelable遇到IOException编写可序列化对象…?相关的知识,希望对你有一定的参考价值。

当尝试将一个对象引用从一个活动传递给另一个时,我遇到了一个问题,我相信罪魁祸首是我拥有的作为该对象字段的列表。我相信的原因是因为当我将列表更改为瞬态时,该对象似乎可以通过,但是当然不能解决我的问题,因为现在在新的Activity中列表为null。

源活动的代码

    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
答案

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

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

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

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

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

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

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

Parcelable 在将对象的实例发送到其他活动时遇到 IOException