I have an android application which is showing strange behavior. I have a method which deserializes object from file. Below is the code I'm using:
public static Object readData(Context context, String fileName)
{
synchronized (context) {
ObjectInputStream input = null;
Object object = null;
if (fileExists(context, fileName)) {
try {
input = new ObjectInputStream(context.openFileInput(fileName));
object = input.readObject();
Log.v(Constant.TAG, "Writable object has been loaded from file "+fileName);
} catch (IOException e) {
Log.e(Constant.TAG, e.getMessage(), e);
} catch (ClassNotFoundException e) {
Log.e(Constant.TAG, e.getMessage(), e);
} finally {
try {
if (input != null)
input.close();
} catch (IOException e) {
Log.e(Constant.TAG, e.getMessage(), e);
}
}
}
return object;
}
}
Normally it works well, but when someone minimize my application and after sometime reopens, it crashes. From crash report I found that it's throwing IllegalArgumentException
in below line from above code
object = input.readObject();
I gone through the documentation of ObjectInputStream.readObject
but it doesn't state circumstances under which it can throw IllegalArgumentException
.
It is only happening when user is bringing app from background. It works perfectly well when app starts (by start I mean when app was not running, not even in background).
PS: there are some crash reports which show ClassCastException
on the same line, which is even stranger as I'm not casting, only reading into an Object
.
Update
Stack trace
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2423)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2483)
at android.app.ActivityThread.access$900 (ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1349)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:148)
at android.app.ActivityThread.main (ActivityThread.java:5441)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:628)
Caused by: java.lang.IllegalArgumentException:
at java.lang.reflect.Field.set (Native Method)
at java.io.ObjectInputStream.readFieldValues (ObjectInputStream.java:1127)
at java.io.ObjectInputStream.defaultReadObject (ObjectInputStream.java:454)
at java.io.ObjectInputStream.readObjectForClass (ObjectInputStream.java:1345)
at java.io.ObjectInputStream.readHierarchy (ObjectInputStream.java:1242)
at java.io.ObjectInputStream.readNewObject (ObjectInputStream.java:1835)
at java.io.ObjectInputStream.readNonPrimitiveContent (ObjectInputStream.java:761)
at java.io.ObjectInputStream.readObject (ObjectInputStream.java:1983)
at java.io.ObjectInputStream.readObject (ObjectInputStream.java:1940)
at com.pixyfisocial.pixyfi.util.IOUtil.readData (IOUtil.java:245)
at com.pixyfisocial.Login.getLoggedInUserInfoFromCache (Login.java:313)
at com.pixyfisocial.Login.startApp (Login.java:124)
at com.pixyfisocial.Login.onCreate (Login.java:98)
at android.app.Activity.performCreate (Activity.java:6303)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2376)