jeudi 2 juillet 2015

Getting NullPointerException with Checkbox

On one Android phone's I'm getting a NullPointerException and on the other one I'm not. I'm trying to save multiple Checkbox states for my privacy settings. As I said on one of the phones it works fine, saves it to sharedPreferences as it should, but on my main phone it crashes as soon as I open the Activity. Anyone see the issue?

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;
    }


}




Aucun commentaire:

Enregistrer un commentaire