我試圖更新是否不是用戶的共享偏好已經檢查一箱不再顯示歡迎屏幕。我訪問我的共享偏好我的onClick偵聽器的按鈕。我得到一個空指針異常,我不知道如何解決它?的NullPointerException在按鈕SharedPreferences ONCLICK
這裏是我的代碼....
public class WelcomeScreenActivity extends Activity {
SharedPreferences mPrefs;
final String welcomeScreenShownPref = "welcomeScreenShown";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcomescreen);
final Button button = (Button) findViewById(R.id.welcomecontinue);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) findViewById(R.id.welcomecheckbox);
if(cb.isChecked()){
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(welcomeScreenShownPref, true);
editor.commit(); // Very important to save the preference
Intent intent = new Intent(WelcomeScreenActivity.this, MainActivity.class);
startActivity(intent);
} else if(!cb.isChecked()){
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(welcomeScreenShownPref, false);
editor.commit(); // Very important to save the preference
Intent intent = new Intent(WelcomeScreenActivity.this, MainActivity.class);
startActivity(intent);
}
}
});
}
}
任何人都可以提供一些線索到這個?
它看起來不像你曾經在那個包含的例子中設置mPrefs。 –
在什麼方面是例外? –