【问题标题】:Android RuntimeException Gson Parsing Exception: Failed to invoke protected java.lang.ClassLoader()Android RuntimeException Gson 解析异常:无法调用受保护的 java.lang.ClassLoader()
【发布时间】:2014-07-05 10:14:20
【问题描述】:

我正在尝试在SharedPreferences 中传递ArrayList of Intent 对象。为此,我使用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);

任何人都可以帮助我如何实现这种行为吗?

提前致谢

【问题讨论】:

  • 你得到答案了吗?
  • 不,我根据自己的要求更改了实现

标签: android android-intent sharedpreferences gson


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-12-06
    • 2021-11-11
    • 2021-10-01
    • 2012-03-30
    • 1970-01-01
    • 2014-05-23
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多