【问题标题】:Static methods in sharedPref crashing my appsharedPref 中的静态方法使我的应用程序崩溃
【发布时间】:2014-11-19 18:40:38
【问题描述】:

我正在使用 sharedPref 在活动之间传递一个字符串。 我正在努力使我的代码更有效,但我遇到了一个我无法弄清楚的奇怪问题。

我在 MainActivity.class 中的代码:

@覆盖 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    //if statement for the first run of the app, else statement uses the defined city.
    if (SharedPref.getCity().equals("")) {

    } else {
        cityText = (TextView) findViewById(R.id.prefCity);
        cityText.setText(SharedPref.getCity());
    }

我的 SharedPref.java :

public class SharedPref {
private static SharedPreferences mSharedPreferences;

//sharedPref Manager.
public SharedPref(final Context context) {
    mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

}

public static void setCity(String item) {
    mSharedPreferences.edit().putString("city", item).apply();
}

public static String getCity() {
    return mSharedPreferences.getString("city", "");
}

}

如您所见,在我的 MainActivity.class 中,我通过“SharedPref.getCity()”访问数据。 直接来自类的意思,这就是我使用静态方法的原因。 但是应用程序崩溃了。 奇怪的是,如果我像这样在 MainActivity 中初始化我的 sharedpref:

SharedPref sharedPref = new SharedPref(getBaseContext());

那么应用程序就可以正常工作,即使 sharedPref 是一个甚至不会使用一次的变量!

我错过了什么?

ps 这是我的 logcat 图片:

提前感谢您的帮助。

【问题讨论】:

  • 我认为因为 SharedPref.getCity() 返回 null,尝试将“if (SharedPref.getCity().equals(""))”更改为“if (SharedPref .getCity()== null )"
  • 您使用 sharedPrefs 而不是意图传递的任何特殊原因?

标签: java android sharedpreferences


【解决方案1】:

最初共享首选项甚至不包含 var "city" 所以最初条件应该是 sharepref.contains("city") 然后在进一步迭代中检查它的值。

【讨论】:

  • 所以这就是空指针异常的原因,因为你的共享偏好中最初不存在城市
  • 你试过什么?最初您需要放置“城市”变量而不是检查它,然后您需要检查它
【解决方案2】:

在您的setCity 方法中,您使用尚未初始化的mSharedPreferences

您需要创建SharedPref 类的新实例,并使setCity 不是静态的。

或者如果您希望setCity 是静态的,您可以这样做:

public static void setCity(Context context, String item) {
    if(mSharedPreferences == null)
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

    mSharedPreferences.edit().putString("city", item).apply();
}

【讨论】:

  • 您好,谢谢您的帮助!刚试了你的代码,还是有错误
【解决方案3】:

看看这段代码:

public static final String SETTINGS_NAME = "MY_SETTINGS";
public static final String FB_TOKEN = "FB_TOKEN";
public static final String LANGUAGE = "LANGUAGE";

public static boolean writeString(Context context, String name, String value)
{
    SharedPreferences settings = context.getSharedPreferences(SETTINGS_NAME, 0);
    Editor editor = settings.edit();
    editor.putString(name, value);

    return editor.commit();
}

public static boolean writeInt(Context context, String name, int value)
{
    SharedPreferences settings = context.getSharedPreferences(SETTINGS_NAME, 0);
    Editor editor = settings.edit();
    editor.putInt(name, value);

    return editor.commit();
}

public static String readString(Context context, String name, String defValue)
{
    SharedPreferences settings = context.getSharedPreferences(SETTINGS_NAME, 0);
    return settings.getString(name, defValue);
}

public static int readInt(Context context, String name, int defValue)
{
    SharedPreferences settings = context.getSharedPreferences(SETTINGS_NAME, 0);
    return settings.getInt(name, defValue);
}

请注意,“SETTINGS_NAME”是您的架构的名称。其他静态名称只是为了让它更容易。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多