在一部 Android 手机上,我收到了 NullPointerException,而在另一部手机上却没有。我正在尝试为我的隐私设置保存多个复选框状态。正如我在其中一部手机上所说的那样,它工作正常,按应有的方式将其保存到 sharedPreferences,但在我的主手机上,一旦我打开 Activity,它就会崩溃。有人看到这个问题吗?
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;
}
}
编辑:
我将 TAG 名称更改为“Different_name”只是为了测试它并且它起作用了。任何人都可以解释为什么它像这样工作 SharedPreferences preferences = getApplicationContext().getSharedPreferences("Different_name", android.content.Context.MODE_PRIVATE);
?
最佳答案
不确定我是否正确理解(因为到目前为止还没有发布日志)但是从您的评论中可以看出
SharedPreferences preferences = getApplicationContext().getSharedPreferences("Different_name", android.content.Context.MODE_PRIVATE);
和你的代码
String TAG = getPackageName();
在某些设备上,您的共享首选项文件名似乎返回为 null
,即使用 getPackageName()
TAG == null
如果您将 TAG
更改为您的 Activity 名称并且它需要是公开的和静态的:
public static final String TAG = PrivacySettings.class.getSimpleName();
您可以在任何地方使用它:
SharedPreferences preferences = getContext().getSharedPreferences(PrivacySettings.TAG, Context.MODE_PRIVATE);
简而言之,似乎 getPackageName()
由于某种原因失败了,所以只需使用一个在任何设备上都不会失败的值,例如当前类(class)名称
关于java - 使用复选框获取 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31194143/