0
我嘗試使用序列化解析在Androud對象使用序列化的時候,但我不斷收到IOExeption錯誤的Android
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,但我不明白爲什麼。 使用序列化的類IM是:
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之間沒有參考。
我如何得到它的工作?
P.S. - TestLogic具有Context類型的成員。 我可能會誤解,但我不認爲Context是Parcelable或Serializable,並且這不是我認爲可以合理存儲和重構的內容。噢,通過它的名字,我猜測QuizActivity是一個活動。這也不起作用,出於與上下文相同的原因(活動是上下文)。 – GreyBeardedGeek
我在Context上使用了transient,但是我得到的錯誤是在CreateUserActivity中,而不是在TestLogic中 –