why does this simple serializable object throw NotSerializableException?
Asked Answered
A

1

1

After getting no answer to my last question, I reformulated the thing to its simplest form. 1 button, 1 clicklistener, 1 serializable object and 1 subroutine to output the serializable object. This code is based on about 6-8 examples I found here on stackoverflow. And yet as simple as it is it still generates this error: W/System.err(228): java.io.NotSerializableException: serobj.testActivity So I challenge you, oh so wise code gurus: why does this code generate this error? And most importantly what do I do to fix it? The entire code followed by log ouput:

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
Aldon answered 17/3, 2012 at 5:8 Comment(0)
S
9

Be cause your inner class is not static, so to exist as a serialized thing, it will need the instance of the outter class and an activity is not serializabled. Make Tester a separate class or make it a static inner class.

Stereoscope answered 17/3, 2012 at 5:14 Comment(2)
Funny how often it is something simple that is just overlooked. Thanks for the speedy reply, Snicolas, that works perfectly.Aldon
Just wanted to share, in one of my classes (which do implement Serializable) I have an inner interface (static). I was having the same NotSerializableException. Looking at your ans I ended up making it extends Serializable and it solved the problem.Aubree

© 2022 - 2024 — McMap. All rights reserved.