在我的上一个问题得不到答案之后,我将事情重新制定为最简单的形式。 1个按钮,1个clicklistener,1个可序列化对象和1个子程序,用于输出可序列化对象。此代码基于我在stackoverflow上找到的大约6-8个示例。然而,它仍然很简单,它仍然会生成此错误:W / System.err(228):java.io.NotSerializableException:serobj.testActivity 所以我挑战你,哦,这么明智的代码大师:为什么这段代码会产生这个错误?最重要的是我该怎么做才能解决它? 整个代码后跟log输出:
package serobj;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import ser.obj.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class testActivity extends Activity {
/** Called when the activity is first created. */
public class Tester implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public String frog;
public Tester(){
frog="frog";
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Testit();
}
});
}
public void Testit(){
Tester test = new Tester();
FileOutputStream fos;
try {
fos =openFileOutput("test.fyl", Context.MODE_WORLD_READABLE);
Log.d("file open","...");
ObjectOutputStream oos = new ObjectOutputStream(fos);
Log.d("ObjOutStr","...");
oos.writeObject(test);
Log.d("tried wO","..."); // never gets here...
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.d ("filenotfound", "filenotfound");
}catch(IOException e){
e.printStackTrace();
Log.d("ioexception", "ioexception");
}
}
}
03-17 04:40:02.691: D/file open(228): ...
03-17 04:40:02.701: D/ObjOutStr(228): ...
03-17 04:40:02.961: W/System.err(228): java.io.NotSerializableException: serobj.testActivity
03-17 04:40:02.971: W/System.err(228): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1547)
03-17 04:40:02.971: W/System.err(228): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1854)
03-17 04:40:02.971: W/System.err(228): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1696)
03-17 04:40:02.971: W/System.err(228): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1660)
03-17 04:40:02.971: W/System.err(228): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
03-17 04:40:02.971: W/System.err(228): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
03-17 04:40:02.981: W/System.err(228): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)
03-17 04:40:02.981: W/System.err(228): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
03-17 04:40:02.981: W/System.err(228): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1854)
03-17 04:40:02.981: W/System.err(228): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1696)
03-17 04:40:02.981: W/System.err(228): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1660)
03-17 04:40:02.981: W/System.err(228): at serobj.testActivity.Testit(testActivity.java:50)
03-17 04:40:03.015: W/System.err(228): at serobj.testActivity$1.onClick(testActivity.java:37)
03-17 04:40:03.015: W/System.err(228): at android.view.View.performClick(View.java:2364)
03-17 04:40:03.015: W/System.err(228): at android.view.View.onTouchEvent(View.java:4179)
03-17 04:40:03.015: W/System.err(228): at android.widget.TextView.onTouchEvent(TextView.java:6541)
03-17 04:40:03.015: W/System.err(228): at android.view.View.dispatchTouchEvent(View.java:3709)
03-17 04:40:03.015: W/System.err(228): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-17 04:40:03.021: W/System.err(228): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-17 04:40:03.021: W/System.err(228): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-17 04:40:03.021: W/System.err(228): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-17 04:40:03.021: W/System.err(228): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-17 04:40:03.021: W/System.err(228): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
03-17 04:40:03.021: W/System.err(228): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
03-17 04:40:03.021: W/System.err(228): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
03-17 04:40:03.021: W/System.err(228): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
03-17 04:40:03.021: W/System.err(228): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
03-17 04:40:03.021: W/System.err(228): at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 04:40:03.031: W/System.err(228): at android.os.Looper.loop(Looper.java:123)
03-17 04:40:03.031: W/System.err(228): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-17 04:40:03.031: W/System.err(228): at java.lang.reflect.Method.invokeNative(Native Method)
03-17 04:40:03.031: W/System.err(228): at java.lang.reflect.Method.invoke(Method.java:521)
03-17 04:40:03.031: W/System.err(228): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-17 04:40:03.031: W/System.err(228): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-17 04:40:03.031: W/System.err(228): at dalvik.system.NativeStart.main(Native Method)
03-17 04:40:03.031: D/ioexception(228): ioexception
03-17 04:51:21.861: D/dalvikvm(56): threadid=15: bogus mon 1+0>0; adjusting
答案 0 :(得分:9)
因为你的内部类不是静态的,所以要作为序列化的东西存在,它将需要outter类的实例并且活动不是serializabled。使Tester成为一个单独的类或使其成为静态内部类。