gpt4 book ai didi

android - 从文件中读取对象抛出 IllegalAccessException

转载 作者:行者123 更新时间:2023-11-29 21:13:52 24 4
gpt4 key购买 nike

我知道我不应该使用这种方法,但出于测试目的我是。我创建了一个扩展 LinearLayout 并实现 Serializable 的类,以便能够将整个布局保存到一个文件中。

向此布局添加 View 后,我将其成功写入文件。但是当回读对象时,我得到一个错误 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)

最佳答案

好像他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());
}

关于android - 从文件中读取对象抛出 IllegalAccessException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21966784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com