我目前设置了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
从其他活动编辑这些resetapp
。为此,我在其他活动的.java
文件上尝试了这一点:
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)
答案 0 :(得分:0)
最有可能的问题是没有注入共享的首字母。
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
// Change preferences
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(“moveToSettings”, true);
editor.commit();
希望它有所帮助!
答案 1 :(得分:0)
您可以为您的应用程序编写一个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();
答案 2 :(得分:0)
当MainActivity
位于前台时,Android不保证NextActivity
仍然存在(即,一旦它们从视图中消失,活动就会被销毁)。
要访问可以从多个活动/服务中检索/保存的首选项,我建议将其放在Android Application类的子类中。
例如,创建:
public class Application extends android.app.Application {
private SharedPreferences mPrefs;
private static Application mApp;
@Override
public void onCreate() {
super.onCreate();
mApp = this;
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
}
public static Application get() {
return mApp;
}
public static SharedPreferences getPrefs() {
return get().mPrefs;
}
}
您的AndroidManifest.xml
还需要android:name
属性来指向此类:
<application
android:name=".Application"
android:icon="@drawable/ic_launcher"
android:logo="@drawable/logo"
android:label="@string/app_name">
然后,您可以从任何活动中获取/设置首选项,如下所示:
// Get a preference
boolean currentValue = Application.getPrefs().getBoolean("moveToSettings", true);
...
// Set a preference
SharedPreferences.Editor edit = Application.getPrefs().edit();
edit.putBoolean("moveToSettings", true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
edit.apply();
} else {
edit.commit();
}
(您也可以将上面的一些设置首选项代码封装在自己的帮助器静态类中。)