2016-06-14 164 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.