我做得对吗?当我按下时,我希望使用SharedPreferences保存卡片阵列(对象),但每当我运行程序时,都会出现错误。或者我应该在onCreate上转换对象吗?
@Override
protected void onPause() {
String json = new Gson().toJson(cards);
SharedPreferences prefs = getSharedPreferences("CARDARRAY",
MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("carddArray", json);
editor.commit();
Toast.makeText(getBaseContext(), "Settings saved", Toast.LENGTH_SHORT)
.show();
super.onPause();
}
logcat的
02-13 23:22:50.091: E/AndroidRuntime(17941): FATAL EXCEPTION: main
02-13 23:22:50.091: E/AndroidRuntime(17941): java.lang.NoClassDefFoundError: com.google.gson.Gson
02-13 23:22:50.091: E/AndroidRuntime(17941): at edu.np.ece.mapg.flashnote.Collections.onPause(Collections.java:118)
02-13 23:22:50.091: E/AndroidRuntime(17941): at android.app.Activity.performPause(Activity.java:5319)
02-13 23:22:50.091: E/AndroidRuntime(17941): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1226)
02-13 23:22:50.091: E/AndroidRuntime(17941): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3142)
02-13 23:22:50.091: E/AndroidRuntime(17941): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3111)
02-13 23:22:50.091: E/AndroidRuntime(17941): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3089)
02-13 23:22:50.091: E/AndroidRuntime(17941): at android.app.ActivityThread.access$800(ActivityThread.java:154)
02-13 23:22:50.091: E/AndroidRuntime(17941): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
02-13 23:22:50.091: E/AndroidRuntime(17941): at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 23:22:50.091: E/AndroidRuntime(17941): at android.os.Looper.loop(Looper.java:137)
02-13 23:22:50.091: E/AndroidRuntime(17941): at android.app.ActivityThread.main(ActivityThread.java:5214)
02-13 23:22:50.091: E/AndroidRuntime(17941): at java.lang.reflect.Method.invokeNative(Native Method)
02-13 23:22:50.091: E/AndroidRuntime(17941): at java.lang.reflect.Method.invoke(Method.java:511)
02-13 23:22:50.091: E/AndroidRuntime(17941): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
02-13 23:22:50.091: E/AndroidRuntime(17941): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
02-13 23:22:50.091: E/AndroidRuntime(17941): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您似乎没有在gradle文件中添加GSON库:
dependencies { compile 'com.google.code.gson:gson:2.2.+' }