我知道我不应该使用这种方法,但出于测试目的,我是。
我创建了一个扩展LinearLayout
并实现Serializable
的类,以便能够将整个布局保存到文件中。
将视图添加到此布局后,我正在将其成功写入文件。但在回读对象时,我收到错误IllegalAcessException
;有什么方法可以解决这个问题吗?
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);
}
}
请帮忙吗?并提前感谢
完整错误
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)
答案 0 :(得分:2)
好像他ObjectStreamClass正在寻找一个空构造函数来实例化你的自定义类。
// Has to have an empty constructor
if (constructor == null) {
String className = constructorClass != null ? constructorClass.getName() : null;
throw new InvalidClassException(className, "IllegalAccessException");
}
LinearLayout没有空构造函数。尝试添加一个空构造函数
public LinearLayout_Serializable() {
this(//getContextSomehow());
}