gson 字符串转换android对象报错 Can't instantiate abstract class java.lang.ClassLoader

来源:互联网 发布:阿里云 cn域名隐私 编辑:程序博客网 时间:2024/06/01 21:18

在使用gson.fromJson 把字符串转成ResolveInfo对象时

抛出

java.lang.RuntimeException: Unable to start activity ComponentInfo{sk.carlauncher/sk.carlauncher.ui.MainActivity}: java.lang.RuntimeException: Failed to invoke protected java.lang.ClassLoader() with no args
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)

Caused by: java.lang.InstantiationException: Can't instantiate abstract class java.lang.ClassLoader
                                                       at java.lang.reflect.Constructor.newInstance0(Native Method)
                                                       at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
                                                       at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:108)
                                                       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:210) 
                                                       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129) 


这样的错误,单看错误是ClassLoader这个类出的问题,对此类没有研究过。只能stackoverflow 

The way I fixed this issue was to create my own object with the necessary data. It saves space and fixes this issue. The issue arises when you try to serialize android objects.

一个回答者说是解决这个问题的方法是创建我自己需要的对象。后半句是说引起这个问题的原因是你试着序列化一个android的对象。

这正是我的问题所在,ResolveInfo是系统类,那我自定义一个实体类,在里面引用ResolveInfo,这样问题就解决了。


附上地址

https://stackoverflow.com/questions/24585333/android-runtimeexception-gson-parsing-exception-failed-to-invoke-protected-java

阅读全文
0 0