Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android NotSerializableException raises for an object

In my android application I use a file to store license data. And I uses Serialize objects. I create a Device object and read the file details in to the object. Device class implements Serializable.

public class MyDevice implements Serializable {}

But at the start of application it deserialize and store in a MyDevice object. My deserializeObject method is as below.

public MyDevice deserializeObject() {

    File SerialFile = new File(GeoTrackerPaths.FILE_PATH);
    MyDevice AndDeviceIn = new MyDevice();

    if (SerialFile.exists()) {
        try {
            FileInputStream fileIn = new FileInputStream(GeoTrackerPaths.FILE_PATH);
            ObjectInputStream objInput = new ObjectInputStream(fileIn);
            AndDeviceIn = (MyDevice) objInput.readObject();
            objInput.close();
            fileIn.close();

        } catch (Exception e) {

            Log.i("TAG", "Exception during deserialization:" + e.getMessage());
            e.printStackTrace();
            System.exit(0);
        }
    }

    return AndDeviceIn;
}

My serialization code

public void serializeObject(Context context, String phoneModel,
        String androidVersion, String executiveCode, String Key,
        String modelID, String tempKey, int noLogin, String expireDate, String Status) {

    try {
        MyDevice AndDeviceOut = new MyDevice(context, phoneModel,
                androidVersion, new Date(), executiveCode, Key, modelID,
                tempKey, noLogin, expireDate, Status);

        FileOutputStream fileOut = new FileOutputStream(
                GeoTrackerPaths.FILE_PATH);
        ObjectOutputStream objOutput = new ObjectOutputStream(fileOut);
        objOutput.writeObject(AndDeviceOut);
        objOutput.flush();
        objOutput.close();
        fileOut.close();

    } catch (Exception e) {
        Log.i("TAG", "Exception during serialization:" + e.getMessage());
        e.printStackTrace();
        System.exit(0);
    }
}

And I'm calling it as below.

DeviceActivator activate=new DeviceActivator();
activate.serializeObject(Activation.this, phoneModel, androidVersion, txtExe, exeKey, modeilID, tempKey, noLogin, expireDate, Activation_Status);

when I'm running the app following exceptions are raised.

java.io.WriteAbortedException: Read an exception; 
java.io.NotSerializableException: com.geotracker.entity.MyDevice

How can I fix this?

like image 941
Samantha Withanage Avatar asked Oct 02 '22 21:10

Samantha Withanage


1 Answers

It doesn't look like the Android Context object is serializable. You can solve this by declaring the Context object as transient, which you can read about in here in the JDK spec: Link. Basically marking a field as transient means it won't participate in Serialization

So declare your field like this in MyDevice :

private transient Context context;

and you should be good to go!

like image 56
Durandal Avatar answered Oct 06 '22 00:10

Durandal