gpt4 book ai didi

java - Android:SharedPreferences 编辑不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 14:48:58 25 4
gpt4 key购买 nike

我目前有SharedPreferences设置为如果用户检查 checkBox , SharedPreferences将被编辑为:

public class MainActivity extends Activity {

public static SharedPreferences prefs;

public void itemClicked(View v) {
CheckBox checkBox = (CheckBox)v;
prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
if(checkBox.isChecked()) prefs.edit().putBoolean("moveToSettings", false).commit();
else prefs.edit().putBoolean("moveToSettings", true).commit();
}

我希望用户能够编辑这些 SharedPreferences从另一个 Activity ,按按钮 resetapp .为此,这是我在我的 .java 上尝试过的。其他 Activity 的文件:
public class NextActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_NextActivity);

final Button resetapp = (Button)findViewById(R.id.resetapp);

resetapp.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

MainActivity.prefs.edit().putBoolean("moveToSettings", true).commit();

}
});
}

然而,当用户按下按钮 resetapp ,应用程序崩溃而不是更改 SharedPreferences .有谁知道为什么会这样?

这是我的 LogCat 的输出:
06-04 10:20:20.455: W/dalvikvm(2808): threadid=1: thread exiting with uncaught exception (group=0x42d5d140)
06-04 10:20:20.545: E/AndroidRuntime(2808): FATAL EXCEPTION: main
06-04 10:20:20.545: E/AndroidRuntime(2808): Process: com.example.MyFirstApp, PID: 2808
06-04 10:20:20.545: E/AndroidRuntime(2808): java.lang.NullPointerException
06-04 10:20:20.545: E/AndroidRuntime(2808): at com.example.MyFirstApp.NextActivity$1.onClick(NextActivity.java:32)
06-04 10:20:20.545: E/AndroidRuntime(2808): at android.view.View.performClick(View.java:4438)
06-04 10:20:20.545: E/AndroidRuntime(2808): at android.view.View$PerformClick.run(View.java:18422)
06-04 10:20:20.545: E/AndroidRuntime(2808): at android.os.Handler.handleCallback(Handler.java:733)
06-04 10:20:20.545: E/AndroidRuntime(2808): at android.os.Handler.dispatchMessage(Handler.java:95)
06-04 10:20:20.545: E/AndroidRuntime(2808): at android.os.Looper.loop(Looper.java:136)
06-04 10:20:20.545: E/AndroidRuntime(2808): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-04 10:20:20.545: E/AndroidRuntime(2808): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 10:20:20.545: E/AndroidRuntime(2808): at java.lang.reflect.Method.invoke(Method.java:515)
06-04 10:20:20.545: E/AndroidRuntime(2808): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-04 10:20:20.545: E/AndroidRuntime(2808): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-04 10:20:20.545: E/AndroidRuntime(2808): at dalvik.system.NativeStart.main(Native Method)

最佳答案

您可以为您的应用程序编写一个 SharedPrefance 类,并在其中编写一些 setter 和 getter 方法并使用它们。它;管理代码很容易,你永远不会对你的应用程序偏好感到困惑。

    public class Pref {
private static final String USER_ID = "userID";
private static final String PREF_NAME = "Tim";

private static Pref instance;
private SharedPreferences preferences;
private Editor editor;

private Pref(Context context) {
preferences = context.getSharedPreferences(PREF_NAME, 0);
editor = preferences.edit();
}

public static Pref getInstance(Context context) {
return instance == null ? new Pref(context) : instance;
}

public void setUserID(String userID) {
editor.putString(USER_ID, userID).commit();
}

public String getUserID() {
return preferences.getString(USER_ID, "");
}
}

从应用程序中,您只需调用即可访问它
Pref.getInstance(this).setUserID("whatever");
Pref.getInstance(this).getUserID();

关于java - Android:SharedPreferences 编辑不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24039577/

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