我正在尝试成功存储和检索URI的arraylist。我不完全确定在哪里放置'存储'部分,但将它放入onPause是有意义的。当我尝试离开活动时,我的应用程序崩溃了。当我试图将它放入onStop时,我遇到了类似的错误。请帮忙知道我在这里做错了什么。
FATAL EXCEPTION: main
Process: my.package.myapplication, PID: 9357
java.lang.RuntimeException: Unable to pause activity {my.package.myapplication/my.package.myapplication.Person1Screen}: java.lang.RuntimeException: writeValueXml: unable to write value content://com.android.providers.media.documents/document/image%3A49684
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3395)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3354)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3329)
at android.app.ActivityThread.access$1100(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5477)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: writeValueXml: unable to write value content://com.android.providers.media.documents/document/image%3A49684
at com.android.internal.util.XmlUtils.writeValueXml(XmlUtils.java:710)
at com.android.internal.util.XmlUtils.writeValueXml(XmlUtils.java:620)
at com.android.internal.util.XmlUtils.writeSetXml(XmlUtils.java:356)
at com.android.internal.util.XmlUtils.writeValueXml(XmlUtils.java:693)
at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:300)
at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:269)
at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:235)
at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:192)
at android.app.SharedPreferencesImpl.writeToFile(SharedPreferencesImpl.java:600)
at android.app.SharedPreferencesImpl.access$800(SharedPreferencesImpl.java:52)
at android.app.SharedPreferencesImpl$2.run(SharedPreferencesImpl.java:515)
at android.app.SharedPreferencesImpl.enqueueDiskWrite(SharedPreferencesImpl.java:536)
at android.app.SharedPreferencesImpl.access$100(SharedPreferencesImpl.java:52)
at android.app.SharedPreferencesImpl$EditorImpl.commit(SharedPreferencesImpl.java:458)
at my.package.myapplication.Person1Screen.onPause(Person1Screen.java:257)
at android.app.Activity.performPause(Activity.java:6557)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1312)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3381)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3354)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3329)
at android.app.ActivityThread.access$1100(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5477)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我的onPause方法如下。 Person1Screen.java:257是最后的editor.commit行。
@Override
protected void onPause(){
super.onPause();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
Set<String> tempSet = new HashSet(happyList);
editor.putStringSet(HAPPY_LIST, tempSet);
editor.commit();
}
我在onCreate中的检索(如果它有帮助):
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Set<String> tempSet = settings.getStringSet(HAPPY_LIST, null);
if(tempSet != null)
for (String str : tempSet)
happyList.add(Uri.parse(str));
&#34; happyList&#34;属于ArrayList<Uri>
类型。
答案 0 :(得分:2)
您似乎正在执行从HashSet
到Set<String>
的未经检查的通用分配,其中该集实际上包含Uri
个对象。当您尝试将此集写入首选项时,代码会尝试读取String
但实际上会获取Uri
对象,从而导致崩溃。要解决此问题,您应该将Uri
个对象转换回String
s。
所以替换这一行:
Set<String> tempSet = new HashSet(happyList);
使用:
Set<String> tempSet = new HashSet<String>();
for (Uri uri : happyList) {
tempSet.add(uri.toString());
}