Sarthak
Sarthak

Reputation: 1168

Unable to save data to sharedPreferences after clicking on the positive Button of dialog box

I am using MaterialDialogs by afollestad.

Now I have created an object of the dialog and in onPositive method which is similar as onPositiveClick I try to save some data in the sharedPreferences.
But the app stops responding and this is the error I get:

java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available

This my code :

MaterialDialog dialog1 = new MaterialDialog.Builder(ViewContentOfDocument.this)
        .title("Enter Relation Name")
        .customView(R.layout.dialog_add_relation_name, true)
        .positiveText("Ok")
        .autoDismiss(false)
        .onPositive(new MaterialDialog.SingleButtonCallback() {
         @Override
         public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
             RadioGroup radioGroup = (RadioGroup) dialog.getCustomView().findViewById(R.id.relation_name_radioGroup);
             int checked = radioGroup.getCheckedRadioButtonId();
             if (checked != -1) {
                 switch (checked) {
                     case R.id.explicit_relation:
                         relation.setRelationName(relation_name[0]);
                         break;
                     case R.id.implicit_relation:
                         relation.setRelationName(relation_name[1]);
                         break;
                     case R.id.atLex_relation:
                         relation.setRelationName(relation_name[2]);
                         break;
                     case R.id.entRel_relation:
                         relation.setRelationName(relation_name[3]);
                         break;
                 }
                 SharedPreferences.Editor editor=relationSharedPreferences.edit();
                 Gson gson=new Gson();
                 editor.putString(Config.savedRelation,gson.toJson(relation));
                 editor.commit();
                 dialog.dismiss();
             } else {
                 Toast.makeText(ViewContentOfDocument.this, "Enter a relation name", Toast.LENGTH_SHORT).show();
             }
         }
        })
        .negativeText("Cancel")
        .onNegative(new MaterialDialog.SingleButtonCallback() {
         @Override
         public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
             dialog.dismiss();
         }
        })
        .show();

The full error trace:

5.949ms total 188.821ms
06-03 15:20:40.645 1553-1553/com.example.sarthak.ir_annotation_tool E/art: Throwing OutOfMemoryError "Failed to allocate a 47768 byte allocation with 1084116 free bytes and 1058KB until OOM; failed due to fragmentation (required continguous free 49152 bytes where largest contiguous free 45056 bytes)" (recursive case)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art: "main" prio=5 tid=1 Runnable
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:   | group="main" sCount=0 dsCount=0 obj=0x73054000 self=0xf3c25800
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:   | sysTid=1553 nice=0 cgrp=default sched=0/0 handle=0xf774eea0
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:   | state=R schedstat=( 25119433242 418663700 7644 ) utm=2376 stm=134 core=2 HZ=100
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:   | stack=0xff509000-0xff50b000 stackSize=8MB
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:   | held mutexes= "mutator lock"(shared held)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:   native: #00 pc 00005d03  /system/lib/libbacktrace_libc++.so (UnwindCurrent::Unwind(unsigned int, ucontext*)+83)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:   native: #01 pc 00003051  /system/lib/libbacktrace_libc++.so (Backtrace::Unwind(unsigned int, ucontext*)+33)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:   native: #02 pc 003c9b97  /system/lib/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int, char const*, art::mirror::ArtMethod*)+135)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:   native: #03 pc 0038f2c2  /system/lib/libart.so (art::Thread::DumpStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const+290)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:   native: #04 pc 00395bdb  /system/lib/libart.so (art::Thread::ThrowOutOfMemoryError(char const*)+459)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:   native: #05 pc 001f1efb  /system/lib/libart.so (art::gc::Heap::ThrowOutOfMemoryError(art::Thread*, unsigned int, art::gc::AllocatorType)+1243)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:   native: #06 pc 001f5ba5  /system/lib/libart.so (art::gc::Heap::AllocateInternalWithGc(art::Thread*, art::gc::AllocatorType, unsigned int, unsigned int*, unsigned int*, art::mirror::Class**)+2773)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:   native: #07 pc 003962bf  /system/lib/libart.so (art::mirror::Array* art::mirror::Array::Alloc<true>(art::Thread*, art::mirror::Class*, int, unsigned int, art::gc::AllocatorType, bool) (.constprop.212)+1455)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:   native: #08 pc 00396b1a  /system/lib/libart.so (_jobject* art::Thread::CreateInternalStackTrace<false>(art::ScopedObjectAccessAlreadyRunnable const&) const+330)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:   native: #09 pc 003445d9  /system/lib/libart.so (art::Throwable_nativeFillInStackTrace(_JNIEnv*, _jclass*)+57)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:   native: #10 pc 00000f3a  /data/dalvik-cache/x86/system@[email protected] (Java_java_lang_Throwable_nativeFillInStackTrace__+110)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at java.lang.Throwable.nativeFillInStackTrace!(Native method)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at java.lang.Throwable.fillInStackTrace(Throwable.java:166)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at java.lang.Throwable.<init>(Throwable.java:95)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at java.lang.Error.<init>(Error.java:48)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at java.lang.VirtualMachineError.<init>(VirtualMachineError.java:46)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at java.lang.OutOfMemoryError.<init>(OutOfMemoryError.java:44)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at java.lang.Throwable.nativeFillInStackTrace!(Native method)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at java.lang.Throwable.fillInStackTrace(Throwable.java:166)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at java.lang.Throwable.<init>(Throwable.java:95)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at java.lang.Exception.<init>(Exception.java:47)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at java.lang.RuntimeException.<init>(RuntimeException.java:46)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at java.lang.ClassCastException.<init>(ClassCastException.java:42)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at libcore.reflect.ListOfTypes.resolveTypes(ListOfTypes.java:70)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at libcore.reflect.ListOfTypes.getResolvedTypes(ListOfTypes.java:55)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at libcore.reflect.ParameterizedTypeImpl.getActualTypeArguments(ParameterizedTypeImpl.java:45)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:353)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:117)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at com.google.gson.Gson.getAdapter(Gson.java:356)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at com.google.gson.Gson.getAdapter(Gson.java:356)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
06-03 15:20:40.671 1553-1553/com.example.sarthak.ir_annotation_tool E/art:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)

The Gson error keeps on coming 20 times in the error trace

Upvotes: 1

Views: 178

Answers (1)

fixmycode
fixmycode

Reputation: 8506

You're getting that error because your data is too long for storing it on SharedPreferences. SharedPreferences is a key/value storage with very limited space, and maybe your relation object is too big of a String when converted to JSON.

You should try to save your JSON file in some other way. Android offers a lot of ways to save data other than SharedPreferences:

  • Save the relation directly to an SQLite table.
  • Save the relation JSON file to a row in SQLite.
  • Save the JSON object on a file on private storage.
  • Save each field of the relation separately on SharedPreferences.

Upvotes: 2

Related Questions