使用复选框获取 NullPointerException

Getting NullPointerException with Checkbox

在一个 Android phone 上我得到了 NullPointerException 而在另一个上我没有。我正在尝试为我的隐私设置保存多个复选框状态。正如我在其中一个 phone 中所说的那样,它工作正常,按应有的方式将其保存到 sharedPreferences,但在我的主 phone 上,它在我打开 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 和静态:

public static final String TAG = PrivacySettings.class.getSimpleName();

您可以在任何地方使用它:

SharedPreferences preferences = getContext().getSharedPreferences(PrivacySettings.TAG, Context.MODE_PRIVATE);

简而言之,似乎 getPackageName() 由于某种原因失败了,所以只需使用一个在任何设备上都不会失败的值,例如当前class姓名