繁体   English   中英

使用复选框获取NullPointerException

[英]Getting NullPointerException with Checkbox

在一部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首选项= getApplicationContext()。getSharedPreferences(“ Different_name”,android.content.Context.MODE_PRIVATE);

和你的代码

字符串TAG = getPackageName();

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

标记==空

如果您将TAG更改为您的活动名称,并且该名称必须是公开且静态的:

公共静态最终字符串TAG = PrivacySettings.class.getSimpleName();

您可以从以下任何地方使用它:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM