共享首选项空指针异常

时间:2015-08-10 17:54:23

标签: android android-fragments android-activity sharedpreferences

我现在有这个问题已经有一段时间了,每当我试图解决它时,我已经放弃了几个小时。

这是我用来与共享偏好进行交互的课程。

public class AppStatus {

    final static String TAG = "AppStatus";
    private static AppStatus instance = new AppStatus();
    static Context context;
    boolean connected = false;
    public  boolean IF_PAST = false;
    ConnectivityManager connectivityManager;

    public static final String FILE_NAME = "appdata";
    public final String AUTH_KEY = "auth_key";
    public final String TEAM_NAME = "team_name";
    public final String TEAM_LOGO = "team_logo";
    public final String LOGIN_STATUS = "login_status";
    public final String CURRENT_TEAM = "current_team";
    public final String TOTAL_TEAMS = "total_teams";
    public final String USER_NAME = "user_name";
    public final String PASSWORD = "password";
    public final String USERID = "user_id";
    public final String GCM_KEY = "gcm_key";
    public final String NOTIFIER_TEAM_ID = "id";
    public final String NOTIFIER_SCHEDULE_ID = "schedule_id";
    public final boolean IS_FROM_PUSH_NOTIFICATION = false;
    public final String FAMILY_MEMBERS_ID = "family_memebers_id";
    public final String IS_PARENT = "is_parent";
    public final String IS_PUSH_NOTIFICATION_ENABLED = "is_notification_enabled";
    public final String BASE_URL = "https://www.fasfasdf.com";
    public final String IS_USER_API_CALLED = "is_user_api_called";

    public static AppStatus getInstance(Context ctx) {
        context = ctx;
        return instance;
    }

    public Boolean isOnline() {

        try {
            connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo networkInfo = connectivityManager
                    .getActiveNetworkInfo();
            connected = networkInfo != null && networkInfo.isAvailable()
                    && networkInfo.isConnected();
            return connected;

        } catch (Exception e) {
            LogUtils.LOGE(TAG, e.toString());
        }
        return connected;
    }

    public Boolean isRegistered() {
        try {
            if (!getSharedBoolValue(LOGIN_STATUS))
                return false;
            else
                return true;
        } catch (Exception e) {
            LogUtils.LOGE(TAG, e.toString());
        }

        return false;
    }

    public Boolean validateEditText(EditText field) {
        String strField = field.getText().toString();
        if (strField.trim().equals("")) {
            return false;
        }
        return true;
    }

    public String getSharedStringValue(String key) {
        SharedPreferences sp = context.getSharedPreferences("FILE_NAME", 0);
        String value = sp.getString(key, null);
        return value;
    }

    public boolean saveSharedStringValue(String key, String value) {
        SharedPreferences sp = context.getSharedPreferences("FILE_NAME", 0);
        Editor edit = sp.edit();
        edit.putString(key, value);
        return edit.commit();
    }

    public boolean clearSharedData() {
        SharedPreferences sp = context.getSharedPreferences("FILE_NAME", 0);
        Editor edit = sp.edit();
        edit.clear();
        return edit.commit();
    }

    public boolean clearSharedDataWithKey(String key) {
        SharedPreferences sp = context.getSharedPreferences("FILE_NAME", 0);
        Editor edit = sp.edit();
        edit.remove(key);
        return edit.commit();
    }

    public Boolean getSharedBoolValue(String key) {
        SharedPreferences sp = context.getSharedPreferences("FILE_NAME", 0);
        Boolean value = sp.getBoolean(key, false);
        return value;
    }

    public boolean saveSharedBoolValue(String key, Boolean value) {
        SharedPreferences sp = context.getSharedPreferences("FILE_NAME", 0);
        Editor edit = sp.edit();
        edit.putBoolean(key, value);
        return edit.commit();
    }

    public int getSharedIntValue(String key) {
        SharedPreferences sp = context.getSharedPreferences("FILE_NAME", 0);
        int value = sp.getInt(key, 0);
        return value;
    }

    public boolean saveSharedIntValue(String key, int value) {
        SharedPreferences sp = context.getSharedPreferences("FILE_NAME", 0);
        Editor edit = sp.edit();
        edit.putInt(key, value);
        return edit.commit();
    }
}

我遇到的问题是,看似随机共享的首选项会获得零点异常。

我实例化appStatus = AppStatus.getInstance(context);。并以这种方式使用appStatus.getSharedStringValue(appStatus.CURRENT_TEAM);

我可以在不同的片段/活动之间切换几次,没有任何问题,然后无处不在我收到我的错误。

  --------- beginning of crash
08-10 10:28:42.761  25908-25908/com.bonziteamandroid E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.bonziteamandroid, PID: 25908
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
            at com.bonziteamandroid.helpers.AppStatus.getSharedStringValue(AppStatus.java:94)
            at com.bonziteamandroid.annoucement.AnnouncementsFragment.getAnnouncments(AnnouncementsFragment.java:121)
            at com.bonziteamandroid.annoucement.AnnouncementsFragment.onCreateView(AnnouncementsFragment.java:50)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:488)
            at com.bonziteamandroid.BonziNativeActivity$ActionManager.setActionOnChange(BonziNativeActivity.java:1352)
            at com.bonziteamandroid.BonziNativeActivity.onAnnouncementsItemClick(BonziNativeActivity.java:337)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我想知道是否有人有类似的问题?或者或许有一个比这个更有建议的方法,我已经google了,以前的答案似乎并不真正适用。此外,一旦我第一次设置共享首选项,我就不会在大多数视图之间清除/擦除它们,但它们偶尔也会获得空指针异常。

1 个答案:

答案 0 :(得分:5)

在某些情况下,您的上下文似乎为空。更简洁的方法是使用静态方法声明一个类来更新和检索SharedPreferences。这些静态方法中的每一个都将采用上下文参数和要更新的值。像这样:

public class PreferenceUtil {

    public static void setEnabled(final Context context, final boolean enabled) {
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        sharedPref.edit().putBoolean("your_key", enabled).commit();
    }

    public static boolean isEnabled(final Context context) {
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPref.getBoolean("your_key", true);
    }

}