gpt4 book ai didi

java - 在另一个类(class)中使用什么上下文共享偏好?

转载 作者:太空狗 更新时间:2023-10-29 15:59:32 26 4
gpt4 key购买 nike

我不知道如何在另一个不是 fragment 或任何东西的类中使用共享首选项

这是我的主要 Activity 代码:

public class MainActivity extends AppCompatActivity {

Backgrounds backs;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
backs = new Backgrounds(this);
bg = (LinearLayout) findViewById(R.id.background);
bg.setBackgroundColor(getResources().getColor(backs.getBackground()));
}
}

这是我的背景类:

public class Backgrounds {
Integer colors[] = {
R.color.red,
R.color.pink,
R.color.purple,
};

private context;
SharedPreferences sharedPref = context.getSharedPreferences("file", 0);
SharedPreferences.Editor editor = sharedPref.edit();
int i = sharedPref.getInt("background", -1);
public Backgrounds(Context context)
{
this.context = context
}
public int getBackground()
{
i++;
try{
editor.putInt("factIndex", i);
editor.commit();
return colors[i];
}catch (Exception e){
i = 0;
editor.putInt("factIndex", i);
editor.commit();
return colors[i];
}
}
}

我试过很多方法。当我收到此错误时,我几乎可以肯定这是代码上下文部分的问题:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' 对空对象引用

我也尝试过使用 context.getApplicationContext() 但错误是相似的。我认为这可能是因为我需要将 Backs 对象的分配移动到 onCreate() 中,但这仍然不起作用。我已经清理了项目并使用 gradle 同步了文件,但程序在加载之前仍然崩溃。该代码在删除任何 SharedPrefrences 内容时完美运行。

最佳答案

这是用于 Preference 的 Common 类。

public class Preference {
private final static String PREF_FILE = "PREF";

/**
* Set a string shared preference
*
* @param key - Key to set shared preference
* @param value - Value for the key
*/
public static void setSharedPreferenceString(Context context, String key, String value) {
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
editor.apply();
}

/**
* Set a integer shared preference
*
* @param key - Key to set shared preference
* @param value - Value for the key
*/
public static void setSharedPreferenceInt(Context context, String key, int value) {
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(key, value);
editor.apply();
}

/**
* Set a Boolean shared preference
*
* @param key - Key to set shared preference
* @param value - Value for the key
*/
public static void setSharedPreferenceBoolean(Context context, String key, boolean value) {
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(key, value);
editor.apply();
}

/**
* Get a string shared preference
*
* @param key - Key to look up in shared preferences.
* @param defValue - Default value to be returned if shared preference isn't found.
* @return value - String containing value of the shared preference if found.
*/
public static String getSharedPreferenceString(Context context, String key, String defValue) {
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
return settings.getString(key, defValue);
}

/**
* Get a integer shared preference
*
* @param key - Key to look up in shared preferences.
* @param defValue - Default value to be returned if shared preference isn't found.
* @return value - String containing value of the shared preference if found.
*/
public static int getSharedPreferenceInt(Context context, String key, int defValue) {
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
return settings.getInt(key, defValue);
}

/**
* Get a boolean shared preference
*
* @param key - Key to look up in shared preferences.
* @param defValue - Default value to be returned if shared preference isn't found.
* @return value - String containing value of the shared preference if found.
*/
public static boolean getSharedPreferenceBoolean(Context context, String key, boolean defValue) {
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
return settings.getBoolean(key, defValue);
}
}

这是获取和设置首选项的用法

Preference.setSharedPreferenceString(this, "Key", "Value")
Preference.getSharedPreferenceString(this, "Key", "default_Value")

您可以在应用程序的任何地方使用。

关于java - 在另一个类(class)中使用什么上下文共享偏好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41185184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com