2016-06-14 59 views
1

當我以嚴格模式啓動我的應用程序時,我遇到了很多相同的錯誤。仿真器顯示我的 「存儲空間捉迷藏了」很多SharedPreferences錯誤

錯誤:

E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. 
java.lang.Throwable: Explicit termination method 'close' not called 
at dalvik.system.CloseGuard.open(CloseGuard.java:184) 
at java.io.FileOutputStream.<init>(FileOutputStream.java:89) 
at java.io.FileOutputStream.<init>(FileOutputStream.java:72) 
at android.app.SharedPreferencesImpl.createFileOutputStream(SharedPreferencesImpl.java:544) 
at android.app.SharedPreferencesImpl.writeToFile(SharedPreferencesImpl.java:592) 
at android.app.SharedPreferencesImpl.access$800(SharedPreferencesImpl.java:51) 
at android.app.SharedPreferencesImpl$2.run(SharedPreferencesImpl.java:512) 
at android.app.SharedPreferencesImpl.enqueueDiskWrite(SharedPreferencesImpl.java:533) 
at android.app.SharedPreferencesImpl.access$100(SharedPreferencesImpl.java:51) 
at android.app.SharedPreferencesImpl$EditorImpl.commit(SharedPreferencesImpl.java:455) 
at com.google.firebase.iid.zzg.zza(Unknown Source) 
at com.google.firebase.iid.zzd.getToken(Unknown Source) 
at com.google.firebase.iid.FirebaseInstanceId.getToken(Unknown Source) 
at com.google.firebase.iid.FirebaseInstanceId.zzUo(Unknown Source) 
at com.google.firebase.iid.FirebaseInstanceIdService.zza(Unknown Source) 
at com.google.firebase.iid.FirebaseInstanceIdService.zzm(Unknown Source) 
at com.google.firebase.iid.zzb$2.run(Unknown Source) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
at java.lang.Thread.run(Thread.java:818) 
W/SharedPreferencesImpl: writeToFile: Got exception: 
java.io.IOException: write failed: ENOSPC (No space left on device) 
at libcore.io.IoBridge.write(IoBridge.java:502) 
at java.io.FileOutputStream.write(FileOutputStream.java:186) 
at com.android.internal.util.FastXmlSerializer.flushBytes(FastXmlSerializer.java:232) 
at com.android.internal.util.FastXmlSerializer.flush(FastXmlSerializer.java:253) 
at com.android.internal.util.FastXmlSerializer.endDocument(FastXmlSerializer.java:198) 
at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:188) 
at android.app.SharedPreferencesImpl.writeToFile(SharedPreferencesImpl.java:597) 
at android.app.SharedPreferencesImpl.access$800(SharedPreferencesImpl.java:51) 
at android.app.SharedPreferencesImpl$2.run(SharedPreferencesImpl.java:512) 
at android.app.SharedPreferencesImpl.enqueueDiskWrite(SharedPreferencesImpl.java:533) 
at android.app.SharedPreferencesImpl.access$100(SharedPreferencesImpl.java:51) 
at android.app.SharedPreferencesImpl$EditorImpl.commit(SharedPreferencesImpl.java:455) 
at com.google.firebase.iid.zzg.zza(Unknown Source) 
at com.google.firebase.iid.zzd.getToken(Unknown Source) 
at com.google.firebase.iid.FirebaseInstanceId.getToken(Unknown Source) 
at com.google.firebase.iid.FirebaseInstanceId.zzUo(Unknown Source) 
at com.google.firebase.iid.FirebaseInstanceIdService.zza(Unknown Source) 
at com.google.firebase.iid.FirebaseInstanceIdService.zzm(Unknown Source) 
at com.google.firebase.iid.zzb$2.run(Unknown Source) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
at java.lang.Thread.run(Thread.java:818) 
Caused by: android.system.ErrnoException: write failed: ENOSPC (No space left on device) 
at libcore.io.Posix.writeBytes(Native Method) 
at libcore.io.Posix.write(Posix.java:258) 
at libcore.io.BlockGuardOs.write(BlockGuardOs.java:313) 
at libcore.io.IoBridge.write(IoBridge.java:497) 

共享偏好類是:

public class SharedHelper { 

public static boolean setProductFilePath(String path) { 
    return getAppPreference().edit() 
      .putString(Constants.PATH_NAME, path) 
      .commit(); 
} 

public static String getProductFilePath() { 
    return getAppPreference().getString(Constants.PATH_NAME, ""); 
} 

public static boolean setClientFilePath(String path) { 
    return getAppPreference().edit() 
      .putString(Constants.PATH_CLIENT_FILE, path) 
      .commit(); 
} 

public static String getClientFilePath() { 
    return getAppPreference().getString(Constants.PATH_CLIENT_FILE, ""); 
} 

public static boolean setEmail(String path) { 
    return getAppPreference().edit() 
      .putString(Constants.EMAIL, path) 
      .commit(); 
} 

public static String getEmail() { 
    return getAppPreference().getString(Constants.EMAIL, ""); 
} 

public static boolean setQuantityColumns(String path) { 
    return getAppPreference().edit() 
      .putString(Constants.QUANTITY_OF_COLUMNS, path) 
      .commit(); 
} 

public static String getQuantityColumns() { 
    return getAppPreference().getString(Constants.QUANTITY_OF_COLUMNS, ""); 
} 

public static int getListState() { 
    return getAppPreference().getInt(Constants.LIST_STATE, 0); 
} 

public static boolean setListState(int listState) { 
    return getAppPreference().edit() 
      .putInt(Constants.LIST_STATE, listState) 
      .commit(); 
} 


public static SharedPreferences getAppPreference() { 
    return App.newInstance().getSharedPreferences(Constants.APP_PREFERENCE_KEY, Context.MODE_PRIVATE); 
} 

爲什麼我有太多的錯誤? (約20個相同的錯誤)

+0

你把所需的權限?在我看來,你忘了補充一些。 –

+0

是的,我有一切必要的權限: Delphian

+0

您的設備中沒有足夠的空間來寫入文件。檢查以下線程並選擇可能適合您情況的解決方案 –

回答

0

使用助手類並避免複雜性。這是我寫的 - Android-SharedPreferences-Helper

Simplifies usage of the default Android SharedPreferences Class. The developer can do in a few lines of code which otherwise would have required several. Simple to understand as compared to the default class and easy to use.