在Android

时间:2016-11-24 20:19:06

标签: java android serialization

我正在尝试使用Serializable解析Androud中的对象,但我不断获得IOExeption

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.cille_000.laesomondo, PID: 5290
              java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.cille_000.laesomondo.logic.TestLogic)
                  at android.os.Parcel.writeSerializable(Parcel.java:1526)
                  at android.os.Parcel.writeValue(Parcel.java:1474)
                  at android.os.Parcel.writeArrayMapInternal(Parcel.java:723)
                  at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408)
                  at android.os.Bundle.writeToParcel(Bundle.java:1133)
                  at android.os.Parcel.writeBundle(Parcel.java:763)
                  at android.content.Intent.writeToParcel(Intent.java:8655)
                  at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3052)
                  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
                  at android.app.Activity.startActivityForResult(Activity.java:4224)
                  at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
                  at android.app.Activity.startActivityForResult(Activity.java:4183)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856)
                  at android.app.Activity.startActivity(Activity.java:4507)
                  at android.app.Activity.startActivity(Activity.java:4475)
                  at com.example.cille_000.laesomondo.logic.TestLogic.showTextInfo(TestLogic.java:47)
                  at com.example.cille_000.laesomondo.startscreen.TestInfoFragment.onClick(TestInfoFragment.java:44)
                  at android.view.View.performClick(View.java:5610)
                  at android.view.View$PerformClick.run(View.java:22265)
                  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:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: java.io.NotSerializableException: com.example.cille_000.laesomondo.startscreen.CreateUserActivity
                  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1224)
                  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.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:1521)
                  at android.os.Parcel.writeValue(Parcel.java:1474) 
                  at android.os.Parcel.writeArrayMapInternal(Parcel.java:723) 
                  at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408) 
                  at android.os.Bundle.writeToParcel(Bundle.java:1133) 
                  at android.os.Parcel.writeBundle(Parcel.java:763) 
                  at android.content.Intent.writeToParcel(Intent.java:8655) 
                  at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3052) 
                  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518) 
                  at android.app.Activity.startActivityForResult(Activity.java:4224) 
                  at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48) 
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75) 
                  at android.app.Activity.startActivityForResult(Activity.java:4183) 
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856) 
                  at android.app.Activity.startActivity(Activity.java:4507) 
                  at android.app.Activity.startActivity(Activity.java:4475) 
                  at com.example.cille_000.laesomondo.logic.TestLogic.showTextInfo(TestLogic.java:47) 
                  at com.example.cille_000.laesomondo.startscreen.TestInfoFragment.onClick(TestInfoFragment.java:44) 
                  at android.view.View.performClick(View.java:5610) 
                  at android.view.View$PerformClick.run(View.java:22265) 
                  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:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

我可以看到问题出在CreateUserActivity上,但我不明白为什么。 使用Serializable on的类是:

package com.example.cille_000.laesomondo.logic;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.example.cille_000.laesomondo.challengescreen.QuizActivity;
import com.example.cille_000.laesomondo.challengescreen.ShowTextActivity;
import com.example.cille_000.laesomondo.challengescreen.TestResultActivity;
import com.example.cille_000.laesomondo.entities.ReadingTest;
import com.example.cille_000.laesomondo.entities.User;

import java.io.Serializable;
import java.util.ArrayList;

public class TestLogic implements Serializable{
private ReadingTest readingTest;
private Long startTime, stopTime, startPause, stopPause;
private ArrayList<Long> pauses = new ArrayList<>();
private Long totalTime;
private int standardReadingSpeed = 3;
private int correct = 0;
private transient Context context;
private User user;
private int textID;
private QuizActivity quizActivity;


public TestLogic(Context context, User user){
    this.context = context;
    this.user = user;
    readingTest = new ReadingTest(context);
    quizActivity = new QuizActivity();
}

public void showTextInfo(int textID){
    this.textID = textID;
    setText(textID);
    Intent intent = new Intent(context, ShowTextActivity.class);
    Bundle bundle = new Bundle();
    bundle.putSerializable("logic", this);
    intent.putExtras(bundle);
    intent.putExtra("Writer",getWriter());
    intent.putExtra("Resume", getInfo());
    intent.putExtra("Title", getName());
    context.startActivity(intent);
}

public void beginTest(int id){
    setText(id);
    ShowTextActivity showText = new ShowTextActivity();
    showText.setText(getText());
    Intent intent = new Intent(context, showText.getClass());
    Bundle bundle = new Bundle();
    bundle.putSerializable("logic", this);
    intent.putExtras(bundle);
    context.startActivity(intent);
    startTime = System.currentTimeMillis();
}

public void getResult(){
    int xp = calculateXP();
    String result = "Antal korrekte svar: " + correct + "\n" +
            "Du læste teksten på " + totalTime/1000%60 + "sekunder" + "\n" +
            "Du får " + xp + " xp";
    user.addXp(xp);
    TestResultActivity testResultActivity = new TestResultActivity();
    testResultActivity.setInfo(result);
    Intent intent = new Intent(context, testResultActivity.getClass());
    Bundle bundle = new Bundle();
    bundle.putSerializable("logic", this);
    intent.putExtras(bundle);
    context.startActivity(intent);
}

public void setText(int id){
    readingTest.setTextID(id);
}

private String getText(){
    return readingTest.getText();
}

private String getName (){ return readingTest.getName(); }

private String getWriter (){ return readingTest.getWriter(); }

private String getInfo (){ return readingTest.getInfo(); }

public void getQuestion1() {
    quizActivity.firstQuestion(readingTest.getQuestion1());
    Intent intent = new Intent(context, quizActivity.getClass());
    Bundle bundle = new Bundle();
    bundle.putSerializable("logic", this);
    intent.putExtras(bundle);
    context.startActivity(intent);
}

public void getQuestion2() {
    quizActivity.secondQuestion(readingTest.getQuestion2());
}

public void getQuestion3() {
    quizActivity.thirdQuestion(readingTest.getQuestion3());
}


public void beginPause(){
    startPause = System.currentTimeMillis();
}

public void stopPause(){
    stopPause = System.currentTimeMillis();
    pauses.add(stopPause-startPause);
}

public void stopTimer(){
    stopTime = System.currentTimeMillis();
    totalTime = stopTime-startTime;
    for (int i = 0; i< pauses.size(); i++){
        totalTime = totalTime- pauses.get(i);
    }
}

private int calculateXP(){
    long seconds = totalTime/1000%60;
    int readingspeed = readingTest.getWordCount()/standardReadingSpeed;
    int xp = (readingTest.getLix()*correct)+readingspeed-(int) seconds+50;
    if(xp <= 10){return 10;}
    else {return xp;}
}

public void checkAnswer(int question, int answer){
    int correctAnswer = readingTest.getCorrectAnswer(question);
    if(answer == correctAnswer){
        correct++;
    }

}

public int getTextID(){
    return textID;
}
}

TestLogic和CreateUserActivity之间没有任何参考。

我怎样才能让它发挥作用?

1 个答案:

答案 0 :(得分:0)

可能的问题是TestLogic类的一个或多个成员(ReadingTest,User,QuizActivity)或其中一个类的成员不是Serializable。

整个对象图中的所有对象都必须是可序列化的才能使其正常工作。

另外,请注意,虽然可以将Serializable对象放入Parcel,但不建议这样做。每当您创建自己的类以在Android应用程序中使用时,您应该实现Parcelable而不是Serializable