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


}

Edit:

I changed the TAG name to "Different_name" just to test it and it worked. Can anyone explain as to why its working like this SharedPreferences preferences = getApplicationContext().getSharedPreferences("Different_name", android.content.Context.MODE_PRIVATE); ?

Answers


Not sure did I get it properly (since there is no log posted as of now) but from your comment

SharedPreferences preferences = getApplicationContext().getSharedPreferences("Different_name", android.content.Context.MODE_PRIVATE);

and your code

String TAG = getPackageName();

it seems your shared preference file name is returned as null on some devices, i.e. using getPackageName()

TAG == null

If you change your TAG to your activity name and it needs to be public and static:

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

you can use it from anywhere as:

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

In short, it seems getPackageName() fails for some reason so just use a value which will never fail on any device, e.g. current class name


Need Your Help

Protecting arguments containing spaces from eval

linux bash shell unix eval

In order to get eval to work on commands that contain spaces inside one of the parameters, I have only found this to work so far:

Referencing the contents of a memory location. (x86 addressing modes)

assembly x86 masm addressing-mode x86-16

I have a memory location that contains a character that I want to compare with another character (and it's not at the top of the stack so I can't just pop it). How do I reference the contents of a ...