在一個Android手機的我得到一個NullPointerException和另一個我不是。我試圖爲我的隱私設置保存多個複選框狀態。正如我在其中一款手機上所說的那樣,它可以正常工作,將其保存到sharedPreferences中,但在我的主手機上,只要打開「活動」,它就會崩潰。任何人都看到這個問題?獲取NullPointerException與複選框
public class PrivacySettings extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
CheckBox showAge,showLocation,showRelationship,showGender,showFacebookButton;
String TAG = getPackageName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_privacy_settings);
showAge=(CheckBox)findViewById(R.id.cbShowAge);
showAge.setChecked(getFromSP("cbShowAge"));
showAge.setOnCheckedChangeListener(this);
showLocation=(CheckBox)findViewById(R.id.cbShowLocation);
showLocation.setChecked(getFromSP("cbShowLocation"));
showLocation.setOnCheckedChangeListener(this);
showRelationship=(CheckBox)findViewById(R.id.cbShowRelationshipStatus);
showRelationship.setChecked(getFromSP("cbShowRelationship"));
showRelationship.setOnCheckedChangeListener(this);
showGender=(CheckBox)findViewById(R.id.cbShowGender);
showGender.setChecked(getFromSP("cbShowGender"));
showGender.setOnCheckedChangeListener(this);
showFacebookButton=(CheckBox)findViewById(R.id.cbShowFacebookLink);
showFacebookButton.setChecked(getFromSP("cbShowFacebookButton"));
showFacebookButton.setOnCheckedChangeListener(this);
setTitle("Privacy Settings");
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private boolean getFromSP(String key){
SharedPreferences preferences = getApplicationContext().getSharedPreferences(TAG, android.content.Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
private void saveInSp(String key,boolean value){
SharedPreferences preferences = getApplicationContext().getSharedPreferences(TAG, android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.apply();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_privacy_settings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.home) {
NavUtils.navigateUpFromSameTask(this);
}
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.cbShowAge:
saveInSp("cbShowAge",isChecked);
break;
case R.id.cbShowLocation:
saveInSp("cbShowLocation",isChecked);
break;
case R.id.cbShowRelationshipStatus:
saveInSp("cbShowRelationship",isChecked);
break;
case R.id.cbShowGender:
saveInSp("cbShowGender",isChecked);
break;
case R.id.cbShowFacebookLink:
saveInSp("cbShowFacebookButton",isChecked);
break;
}
}
編輯:
我改變了標籤名 「Different_name」 只是爲了測試它和它的工作。任何人都可以解釋爲什麼它的工作是這樣的SharedPreferences preferences = getApplicationContext().getSharedPreferences("Different_name", android.content.Context.MODE_PRIVATE);
?
手機是否具有不同的屏幕分辨率? – ryguy
發佈logcat。 –
是的,但是這與什麼有關?我會等一下。 – Ivan