Reputation: 682
I know that I shouldn't use this approach but for testing purposes I am.
I created a class that extends LinearLayout
and implements Serializable
to be able to save the whole layout to a file.
After adding views to this layout I'm writting it successfully to a file. But when reading back the object I get an error IllegalAcessException
; Is there any way to solve this issue?
CustomLayout.class
public class CustomLayout extends LinearLayout implements Serializable{
public static final long serialVersionUID = 1000;
public LinearLayout_Serializable(Context context) {
super(context);
}
public LinearLayout_Serializable(Context context, AttributeSet attributeset){
super(context,attributeset);
}
}
Any help please? and thanks in advance
Full error
02-23 12:06:01.165: W/System.err(21887): java.io.InvalidClassException: android.widget.LinearLayout; IllegalAccessException
02-23 12:06:01.165: W/System.err(21887): at java.io.ObjectStreamClass.resolveConstructorClass(ObjectStreamClass.java:692)
02-23 12:06:01.165: W/System.err(21887): at java.io.ObjectStreamClass.newInstance(ObjectStreamClass.java:653)
02-23 12:06:01.165: W/System.err(21887): at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1819)
02-23 12:06:01.165: W/System.err(21887): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
02-23 12:06:01.165: W/System.err(21887): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
02-23 12:06:01.165: W/System.err(21887): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2006)
02-23 12:06:01.165: W/System.err(21887): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1963)
02-23 12:06:01.165: W/System.err(21887): at com.example.fastlayout.MainActivity.readFromFile(MainActivity.java:232)
02-23 12:06:01.165: W/System.err(21887): at com.example.fastlayout.MainActivity.onCreate(MainActivity.java:102)
02-23 12:06:01.165: W/System.err(21887): at android.app.Activity.performCreate(Activity.java:5372)
02-23 12:06:01.165: W/System.err(21887): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-23 12:06:01.165: W/System.err(21887): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
02-23 12:06:01.165: W/System.err(21887): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
02-23 12:06:01.165: W/System.err(21887): at android.app.ActivityThread.access$700(ActivityThread.java:159)
02-23 12:06:01.165: W/System.err(21887): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
02-23 12:06:01.170: W/System.err(21887): at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 12:06:01.170: W/System.err(21887): at android.os.Looper.loop(Looper.java:176)
02-23 12:06:01.170: W/System.err(21887): at android.app.ActivityThread.main(ActivityThread.java:5419)
02-23 12:06:01.170: W/System.err(21887): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 12:06:01.170: W/System.err(21887): at java.lang.reflect.Method.invoke(Method.java:525)
02-23 12:06:01.170: W/System.err(21887): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
02-23 12:06:01.170: W/System.err(21887): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
02-23 12:06:01.170: W/System.err(21887): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 2
Views: 538
Reputation: 8004
Seems like he ObjectStreamClass is looking for an empty constructor to instantiate your custom class.
// Has to have an empty constructor
if (constructor == null) {
String className = constructorClass != null ? constructorClass.getName() : null;
throw new InvalidClassException(className, "IllegalAccessException");
}
LinearLayout does not have an empty constructor. Try just adding an empty constructor
public LinearLayout_Serializable() {
this(//getContextSomehow());
}
Upvotes: 3