在SharedPreferences中读取自定义ArrayList会调用运行时异常

时间:2018-05-13 17:46:55

标签: android json sharedpreferences classloader runtimeexception

我正在制作像Nike这样的应用程序运行应用程序。 TrackerService类是一个Service,它在stop()上保存锻炼记录,然后从 Process: smu.ac.kr.johnber, PID: 1276 java.lang.RuntimeException: Failed to invoke protected java.lang.ClassLoader() with no args at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:111) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:210) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61) at com.google.gson.Gson.fromJson(Gson.java:887) at com.google.gson.Gson.fromJson(Gson.java:852) at com.google.gson.Gson.fromJson(Gson.java:801) at smu.ac.kr.johnber.run.TrackerService.resume(TrackerService.java:284) at smu.ac.kr.johnber.run.TrackerService.start(TrackerService.java:227) at smu.ac.kr.johnber.run.RunningFragment$1.onServiceConnected(RunningFragment.java:226) at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1453) at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1481) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 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)  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)  at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)  at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)  at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)  at com.google.gson.Gson.fromJson(Gson.java:887)  at com.google.gson.Gson.fromJson(Gson.java:852)  at com.google.gson.Gson.fromJson(Gson.java:801)  at smu.ac.kr.johnber.run.TrackerService.resume(TrackerService.java:284)  at smu.ac.kr.johnber.run.TrackerService.start(TrackerService.java:227)  at smu.ac.kr.johnber.run.RunningFragment$1.onServiceConnected(RunningFragment.java:226)  at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1453)  at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1481)  at android.os.Handler.handleCallback(Handler.java:751)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  上的sharedPreferences重新加载数据。在我搜索了几个解决方案之后,我实现了将ArrayList解析为Json的方式。首先尝试stop()恢复(),锻炼记录成功恢复但是当第二次尝试stop()恢复()时,发生了RunTimeException。

我该如何解决这个问题?

 <div id="technicalstudiesSubTabs">
    <ul class="tabsBoxSubMenu techStudiesTabsSubMenu  innerPage "      id="timePeriodsWidget"  data-view="normal">
        <li pairid="1" data-period="60" class="first"><a href="javascript:void(0);">1 min.</a></li>
        <li pairid="1" data-period="300" class=""><a href="javascript:void(0);">5 mins</a></li>
        <li pairid="1" data-period="900" class=""><a href="javascript:void(0);">15 mins</a></li>
        <li pairid="1" data-period="1800" class=""><a href="javascript:void(0);">30 mins</a></li>
        <li pairid="1" data-period="3600" class="selected "><a href="javascript:void(0);">Hourly</a></li>
        <li pairid="1" data-period="18000" class=""><a href="javascript:void(0);">5 Hours</a></li>
        <li pairid="1" data-period="86400" class=""><a href="javascript:void(0);">Daily</a></li>
        <li pairid="1" data-period="week" class=""><a href="javascript:void(0);">Weekly</a></li>
        <li pairid="1" data-period="month" class=""><a href="javascript:void(0);">Monthly</a></li>
   </ul>
 </div>

05-14 01:43:03.974 1276-1276 / smu.ac.kr.johnber E / AndroidRuntime:FATAL EXCEPTION:main

{{1}}

1 个答案:

答案 0 :(得分:1)

相反用它的构造函数创建Gson对象,我做了GsonBuilder并附加了serializeNulls()它成功地工作了

GsonBuilder builder = new GsonBuilder();
builder.serializeNulls();