-1

I am trying to stash my ArrayList of custom object into SharedPreference but I'm getting errors and at a total loss what to do. Please can someone help me?

I've got my ArrayList of custom object which I then stash into another object so that I can create Json String object to store into sharedPreferences.Thus storing my 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...");

My classes:

public class SearchTerm implements Serializable {

private String searchTerm;

// Constructor
public SearchTerm(String searchTerm) {
    this.searchTerm = searchTerm;

...................

and:

public class ObjectForArrayList_searchTerms implements Serializable {
private ArrayList<SearchTerm> searchterms;

public ObjectForArrayList_searchTerms(ArrayList<SearchTerm> searchTerm) {
    this.searchterms = searchTerm;
}

StackTrace:

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)
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • Have you added `GSON` library to your project ? – Shobhit Puri Nov 13 '14 at 22:44
  • yes i have. the code does not give any red lines. – user3274549 Nov 13 '14 at 22:45
  • I have gson.2.3.jar present in my Referenced Libraries of my project – user3274549 Nov 13 '14 at 22:46
  • 1
    Try Checking the checkbox next to the lib (gson-2.3.jar) in: `Project Properties` -> `Java Build Path` -> `Order and Export` tab. Then do a clean/build. – Shobhit Puri Nov 13 '14 at 22:48
  • ok. The box was not checked and so i checked it. upon re-launching the app I got the folloeing message: " could not find class 'com.google.gson.Gson', referenced from method ..... – user3274549 Nov 13 '14 at 23:03
  • okay. You relaunched the app after cleaning the project, right? – Shobhit Puri Nov 13 '14 at 23:04
  • im sorry, how/what do you mean by "cleaning the project" – user3274549 Nov 13 '14 at 23:05
  • 1
    I mean before running the project, did you click on `Project` -> `Clean ..` ? – Shobhit Puri Nov 13 '14 at 23:06
  • by the way, what does cleaning the project do exactly? – user3274549 Nov 13 '14 at 23:11
  • 1
    @user3274549 pretty much the same as any make file that has a clean option. It remove the objects that was compiled for runtime or any other runtime generated files etc. Usually you would do a clean and build, ie, clean your project and build. – Janpan Nov 13 '14 at 23:14
  • +1 to what Janpan said. http://stackoverflow.com/questions/4549161/function-of-project-clean-in-eclipse is a good read for you. – Shobhit Puri Nov 13 '14 at 23:15
  • possible duplicate of [Android error - Caused by: java.lang.NoClassDefFoundError: android.support.v4.util.SparseArrayCompat](http://stackoverflow.com/questions/16641739/android-error-caused-by-java-lang-noclassdeffounderror-android-support-v4-ut) and only FSM knows how many others questions here on SO – Selvin Nov 13 '14 at 23:16

1 Answers1

0

Check the checkbox next to the lib (gson-2.3.jar) in: Project Properties -> Java Build Path -> Order and Export tab. Then do a clean -> build.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124