Android RuntimeException Gson解析异常:无法调用受保护的java.lang.ClassLoader()

时间:2014-07-05 10:14:21

标签: android android-intent sharedpreferences gson

我正在尝试传递ArrayList of Intent中的SharedPreferences个对象。为此,我使用gsonArrayList of Intents转换为String,然后设置共享首选项。设置共享首选项已成功完成,但在检索时,我收到以下异常:

异常

07-06 00:50:05.712: E/AndroidRuntime(22674): FATAL EXCEPTION: main
07-06 00:50:05.712: E/AndroidRuntime(22674): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tzoomers.birthdaysdiary/com.tzoomers.birthdaysdiary.NotificationView}: java.lang.RuntimeException: Failed to invoke protected java.lang.ClassLoader() with no args
07-06 00:50:05.712: E/AndroidRuntime(22674):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
07-06 00:50:05.712: E/AndroidRuntime(22674):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
07-06 00:50:05.712: E/AndroidRuntime(22674):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
07-06 00:50:05.712: E/AndroidRuntime(22674):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
07-06 00:50:05.712: E/AndroidRuntime(22674):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 00:50:05.712: E/AndroidRuntime(22674):    at android.os.Looper.loop(Looper.java:154)
07-06 00:50:05.712: E/AndroidRuntime(22674):    at android.app.ActivityThread.main(ActivityThread.java:4944)
07-06 00:50:05.712: E/AndroidRuntime(22674):    at java.lang.reflect.Method.invokeNative(Native Method)
07-06 00:50:05.712: E/AndroidRuntime(22674):    at java.lang.reflect.Method.invoke(Method.java:511)
07-06 00:50:05.712: E/AndroidRuntime(22674):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-06 00:50:05.712: E/AndroidRuntime(22674):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-06 00:50:05.712: E/AndroidRuntime(22674):    at dalvik.system.NativeStart.main(Native Method)
07-06 00:50:05.712: E/AndroidRuntime(22674): Caused by: java.lang.RuntimeException: Failed to invoke protected java.lang.ClassLoader() with no args
07-06 00:50:05.712: E/AndroidRuntime(22674):    at com.google.gson.internal.ConstructorConstructor$2.construct(ConstructorConstructor.java:94)

设置SharedPreferences

ArrayList<Intent> notificationIntent = new ArrayList<Intent>();

SharedPreferences.Editor editor = prefs.edit();
String connectionsJSONString = new Gson().toJson(notificationIntent);
editor.putString("intents", connectionsJSONString);
editor.commit();

从SharedPreference

重播
String connectionsJSONString = PreferenceManager.getDefaultSharedPreferences(this)
                                        .getString("intents", null);
Type type = new TypeToken < ArrayList < Intent >> () {}.getType();
notificationsIntent = new Gson().fromJson(connectionsJSONString, type);

任何人都可以帮助我,我怎样才能实现这种行为?

提前致谢

1 个答案:

答案 0 :(得分:0)

我解决此问题的方法是使用必要的数据创建自己的对象。它节省了空间并修复了这个问题。当您尝试序列化android对象时会出现此问题。