很多SharedPreferences错误

时间:2016-06-14 11:16:04

标签: android 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个相同的错误)

1 个答案:

答案 0 :(得分:0)

使用帮助程序类并避免复杂性。这是我写的一个 - Android-SharedPreferences-Helper

  

简化默认Android SharedPreferences类的使用。该   开发人员可以用几行代码来完成   需要几个。与默认相比,易于理解   类和易于使用。