我正在尝试将自定义对象的ArrayList存储到SharedPreference中,但我收到错误并完全失去了该怎么做。请有人帮帮我吗?
我已经获得了自定义对象的ArrayList,然后我将其藏匿到另一个对象中,这样我就可以创建Json String对象来存储到sharedPreference中。这样就可以存储我的ArrayList。
Here is the code that does this:
ArrayList<SearchTerm> searchterms = new ArrayList<SearchTerm>();
searchterms.add(new SearchTerm(query));
Log.d(TAG, "object with attrb query added to ArrayList");
//Placing ArrayList into another object to store in SharedPreferences
searchterms_Wrapper = new ObjectForArrayList_searchTerms(searchterms);
Log.d(TAG, "ArrayList added to another object ");
// saving...
Gson gson = new Gson();
String objectContainingSerchTerms = gson.toJson(searchterms_Wrapper);
PreferenceManager.getDefaultSharedPreferences(getActivity())
.edit()
.putString(SEARCH_TERM_ARRAY_LIST, objectContainingSerchTerms)
.commit();
Log.d(TAG, "searchTerm ArrayList saved...");
我的课程:
public class SearchTerm implements Serializable {
private String searchTerm;
// Constructor
public SearchTerm(String searchTerm) {
this.searchTerm = searchTerm;
...................
和
public class ObjectForArrayList_searchTerms implements Serializable {
private ArrayList<SearchTerm> searchterms;
public ObjectForArrayList_searchTerms(ArrayList<SearchTerm> searchTerm) {
this.searchterms = searchTerm;
}
堆栈跟踪:
11-13 22:31:49.971: E/AndroidRuntime(24689): FATAL EXCEPTION: main
11-13 22:31:49.971: E/AndroidRuntime(24689): Process: com.example.lifesci_pubmed, PID: 24689
11-13 22:31:49.971: E/AndroidRuntime(24689): java.lang.NoClassDefFoundError: com.google.gson.Gson
11-13 22:31:49.971: E/AndroidRuntime(24689): at com.android.lifesci_pubmed.ArticleListFragment_develop.onOptionsItemSelected(ArticleListFragment_develop.java:428)
11-13 22:31:49.971: E/AndroidRuntime(24689): at android.support.v4.app.Fragment.performOptionsItemSelected(Fragment.java:1612)
11-13 22:31:49.971: E/AndroidRuntime(24689): at android.support.v4.app.FragmentManagerImpl.dispatchOptionsItemSelected(FragmentManager.java:2018)
11-13 22:31:49.971: E/AndroidRuntime(24689): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:379)
11-13 22:31:49.971: E/AndroidRuntime(24689): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1012)
11-13 22:31:49.971: E/AndroidRuntime(24689): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:741)
11-13 22:31:49.971: E/AndroidRuntime(24689): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
11-13 22:31:49.971: E/AndroidRuntime(24689): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:884)
11-13 22:31:49.971: E/AndroidRuntime(24689): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
11-13 22:31:49.971: E/AndroidRuntime(24689): at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:177)
11-13 22:31:49.971: E/AndroidRuntime(24689): at android.widget.AdapterView.performItemClick(AdapterView.java:299)
11-13 22:31:49.971: E/AndroidRuntime(24689): at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
11-13 22:31:49.971: E/AndroidRuntime(24689): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
11-13 22:31:49.971: E/AndroidRuntime(24689): at android.widget.AbsListView$3.run(AbsListView.java:3645)
11-13 22:31:49.971: E/AndroidRuntime(24689): at android.os.Handler.handleCallback(Handler.java:733)
11-13 22:31:49.971: E/AndroidRuntime(24689): at android.os.Handler.dispatchMessage(Handler.java:95)
11-13 22:31:49.971: E/AndroidRuntime(24689): at android.os.Looper.loop(Looper.java:136)
11-13 22:31:49.971: E/AndroidRuntime(24689): at android.app.ActivityThread.main(ActivityThread.java:5086)
11-13 22:31:49.971: E/AndroidRuntime(24689): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 22:31:49.971: E/AndroidRuntime(24689): at java.lang.reflect.Method.invoke(Method.java:515)
11-13 22:31:49.971: E/AndroidRuntime(24689): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-13 22:31:49.971: E/AndroidRuntime(24689): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-13 22:31:49.971: E/AndroidRuntime(24689): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
选中lib {gson-2.3.jar)旁边的复选框:Project Properties
- &gt; Java Build Path
- &gt; Order and Export
标签。然后做一个clean
- &gt; build
。