gpt4 book ai didi

android - 为什么这个简单的可序列化对象抛出 NotSerializableException?

转载 作者:行者123 更新时间:2023-11-29 14:28:42 24 4
gpt4 key购买 nike

在我的最后一个问题没有得到答案后,我将这件事重新表述为最简单的形式。 1 个按钮、1 个点击监听器、1 个可序列化对象和 1 个用于输出可序列化对象的子例程。此代码基于我在 stackoverflow 上找到的大约 6-8 个示例。然而尽管如此简单,它仍然会生成此错误:W/System.err(228): java.io.NotSerializableException: serobj.testActivity所以我向你挑战,哦,如此聪明的代码大师:为什么这段代码会产生这个错误?最重要的是我该怎么做才能解决它?完整代码后跟日志输出:

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

最佳答案

因为你的内部类不是静态的,所以要作为序列化的东西存在,它需要外部类的实例并且 Activity 不是可序列化的。将 Tester 设为单独的类或将其设为静态内部类。

关于android - 为什么这个简单的可序列化对象抛出 NotSerializableException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9747448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com