当我尝试保存通知对象时遇到问题。我尝试过序列化(但通知不可序列化)使用包裹没有运气,然后我找到了一个有前途的方法:GSON
但是在实施并包括图书馆在内的几个问题之后,我再次陷入困境。
这段代码似乎工作正常:
Gson gson = new Gson();
String json = gson.toJson(notification);
但是当我尝试重新构建通知对象时,它会崩溃:
Notification notification = gson.fromJson(json, Notification.class);
这是崩溃日志:
07-03 13:13:55.930: E/AndroidRuntime(12324): FATAL EXCEPTION: main
07-03 13:13:55.930: E/AndroidRuntime(12324): java.lang.RuntimeException: Unable to resume activity {org.example.hello/org.example.hello.Hello}: java.lang.RuntimeException: Failed to invoke private android.widget.RemoteViews$Action() with no args
07-03 13:13:55.930: E/AndroidRuntime(12324): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3014)
07-03 13:13:55.930: E/AndroidRuntime(12324): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3055)
07-03 13:13:55.930: E/AndroidRuntime(12324): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
07-03 13:13:55.930: E/AndroidRuntime(12324): at android.app.ActivityThread.access$600(ActivityThread.java:151)
07-03 13:13:55.930: E/AndroidRuntime(12324): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
07-03 13:13:55.930: E/AndroidRuntime(12324): at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 13:13:55.930: E/AndroidRuntime(12324): at android.os.Looper.loop(Looper.java:155)
07-03 13:13:55.930: E/AndroidRuntime(12324): at android.app.ActivityThread.main(ActivityThread.java:5485)
07-03 13:13:55.930: E/AndroidRuntime(12324): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 13:13:55.930: E/AndroidRuntime(12324): at java.lang.reflect.Method.invoke(Method.java:511)
07-03 13:13:55.930: E/AndroidRuntime(12324): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
07-03 13:13:55.930: E/AndroidRuntime(12324): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
07-03 13:13:55.930: E/AndroidRuntime(12324): at dalvik.system.NativeStart.main(Native Method)
07-03 13:13:55.930: E/AndroidRuntime(12324): Caused by: java.lang.RuntimeException: Failed to invoke private android.widget.RemoteViews$Action() with no args
07-03 13:13:55.930: E/AndroidRuntime(12324): at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:107)
07-03 13:13:55.930: E/AndroidRuntime(12324): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:162)
07-03 13:13:55.930: E/AndroidRuntime(12324): at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
07-03 13:13:55.930: E/AndroidRuntime(12324): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
07-03 13:13:55.930: E/AndroidRuntime(12324): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
07-03 13:13:55.930: E/AndroidRuntime(12324): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
07-03 13:13:55.930: E/AndroidRuntime(12324): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
07-03 13:13:55.930: E/AndroidRuntime(12324): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
07-03 13:13:55.930: E/AndroidRuntime(12324): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
07-03 13:13:55.930: E/AndroidRuntime(12324): at com.google.gson.Gson.fromJson(Gson.java:803)
07-03 13:13:55.930: E/AndroidRuntime(12324): at com.google.gson.Gson.fromJson(Gson.java:768)
07-03 13:13:55.930: E/AndroidRuntime(12324): at com.google.gson.Gson.fromJson(Gson.java:717)
07-03 13:13:55.930: E/AndroidRuntime(12324): at com.google.gson.Gson.fromJson(Gson.java:689)
07-03 13:13:55.930: E/AndroidRuntime(12324): at ie.elementsoftware.WaveNotification.setCustomNotification(WaveNotification.java:92)
07-03 13:13:55.930: E/AndroidRuntime(12324): at org.example.hello.Hello.onResume(Hello.java:94)
07-03 13:13:55.930: E/AndroidRuntime(12324): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1266)
07-03 13:13:55.930: E/AndroidRuntime(12324): at android.app.Activity.performResume(Activity.java:5148)
07-03 13:13:55.930: E/AndroidRuntime(12324): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2997)
07-03 13:13:55.930: E/AndroidRuntime(12324): ... 12 more
07-03 13:13:55.930: E/AndroidRuntime(12324): Caused by: java.lang.InstantiationException: can't instantiate class android.widget.RemoteViews$Action
07-03 13:13:55.930: E/AndroidRuntime(12324): at java.lang.reflect.Constructor.constructNative(Native Method)
07-03 13:13:55.930: E/AndroidRuntime(12324): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
07-03 13:13:55.930: E/AndroidRuntime(12324): at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:104)
07-03 13:13:55.930: E/AndroidRuntime(12324): ... 29 more
我错过了什么吗?或者只是我无法将 android 类转换为 JSON?如果问题是这样,我是否有另一种方法来存储对象,而不是手动存储通知对象中包含的每个可能的参数?
谢谢!