【问题标题】:Getting NullPointerException with Checkbox使用复选框获取 NullPointerException
【发布时间】:2015-09-20 13:08:57
【问题描述】:

在一部 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);

【问题讨论】:

  • 手机有不同的屏幕分辨率吗?
  • 是的,他们有,但这与什么有什么关系?我会等一下。
  • 由于某种原因,logcat 无法正确复制到 stackoverflow。

标签: java android checkbox nullpointerexception sharedpreferences


【解决方案1】:

不确定我是否正确获得它(因为目前没有发布日志)但来自您的评论

SharedPreferences 首选项 = getApplicationContext().getSharedPreferences("Different_name", android.content.Context.MODE_PRIVATE);

和你的代码

字符串标签 = getPackageName();

您的共享首选项文件名似乎在某些设备上返回为null,即使用getPackageName()

标签 == 空

如果您将 TAG 更改为您的活动名称,并且它需要是公开且静态的:

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

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

SharedPreferences 首选项 = getContext().getSharedPreferences(PrivacySettings.TAG, Context.MODE_PRIVATE);

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

【讨论】:

    猜你喜欢
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 2018-08-25
    • 2011-12-18
    • 1970-01-01
    • 2011-09-06
    相关资源
    最近更新 更多