2016-08-07 31 views
1

我試圖成功地存儲和檢索URI的數組列表。我不完全確定將'存儲'部分放在哪裏,但將它放在onPause中是有意義的。當我嘗試離開活動時,我的應用崩潰了。當我嘗試將它放入onStop時,我收到了類似的錯誤。請幫助知道我在這裏做錯了什麼。java.lang.RuntimeException:writeValueXml:無法寫入值

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

「happyList」 是ArrayList<Uri>型。

回答

1

看起來您正在執行一個未經檢查的通用任務,從HashSetSet<String>,其中該實際上包含Uri對象。當您嘗試將此設置寫入首選項時,代碼嘗試讀取String,但實際上得到一個Uri對象,導致崩潰。要解決這個問題,你應該將你的Uri對象轉換回String

所以替換此行:你的貼

Set<String> tempSet = new HashSet<String>(); 
for (Uri uri : happyList) { 
    tempSet.add(uri.toString()); 
} 
+0

我想通了這一點之前:

Set<String> tempSet = new HashSet(happyList);

用。完全按照你寫的完成,沒有崩潰。 – rafvasq