android - 单击对话框的正按钮后无法将数据保存到 sharedPreferences

标签 android sharedpreferences android-alertdialog

我正在使用 MaterialDialogs by afollestad .

现在我已经创建了一个对话框的对象,并且在类似于 onPositiveClickonPositive 方法中我尝试在 sharedPreferences 中保存一些数据>.
但是应用程序停止响应,这是我得到的错误:

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

这是我的代码:

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();

完整的错误跟踪:

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@framework@boot.oat (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)

Gson错误在错误跟踪中不断出现20次

最佳答案

您收到该错误是因为您的数据太长,无法将其存储在 SharedPreferences 上。 SharedPreferences 是一个空间非常有限的键/值存储,当转换为 JSON 时,您的关系对象可能太大了。

您应该尝试以其他方式保存您的 JSON 文件。除了 SharedPreferences 之外,Android 还提供了很多保存数据的方法:

  • 将关系直接保存到 SQLite 表中。
  • 将关系 JSON 文件保存到 SQLite 中的一行。
  • 将 JSON 对象保存在私有(private)存储的文件中。
  • 在 SharedPreferences 上分别保存关系的每个字段。

关于android - 单击对话框的正按钮后无法将数据保存到 sharedPreferences,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37629002/

相关文章:

android - 如何在 Android 中使用 Intent 启动闹钟屏幕?

java - 共享首选项不起作用

java - 将 ArrayList 转换为字符串以存储在共享首选项中

android textview 文本方向现在响应变化

android - Android 上的 Azure MobileServicePushFailedException

android - 如何为 Xamarin 库项目(iOS 和 Android)运行单元测试?

java - 离开 android 中的 Activity 后如何保留 ListView ?

带圆角的 Android AlertDialog : rectangle seen below corners

android - 如何在Alert Dialog中设置EditText的dp左右边距

android - AlertDialog 弹出两次