aufzurufen Ich habe eine Einstellungsaktivität erstellt und ich möchte den Switch Button mit SharedPreferences speichern, aber ich bekomme einen Fehler (siehe Titel), wenn ich starten Sie die Aktivität:Versuch, virtuelle Methode 'void android.widget.Switch.setChecked (boolean)' auf einem Nullobjekt Referenz
Der Fehler ist in Zeile 13
Der Code:
public class SettingsActivity extends Activity {
private Switch switchPushNotifications;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
SharedPreferences sharedPreferences = getSharedPreferences("Settings", MODE_PRIVATE);
switchPushNotifications.setChecked(sharedPreferences.getBoolean("getPushNotifications", true));
switchPushNotifications = (Switch) findViewById(R.id.switchPush);
switchPushNotifications.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Log.d("PN", "Push Notifications are currently ON");
Pushbots.sharedInstance().setPushEnabled(true);
Pushbots.sharedInstance().register();
SharedPreferences.Editor sharedPreferencesEditor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
sharedPreferencesEditor.putBoolean("getPushNotifications", true);
sharedPreferencesEditor.commit();
}
else {
Log.d("PN", "Push Notifications are currently OFF");
Pushbots.sharedInstance().setPushEnabled(false);
Pushbots.sharedInstance().unRegister();
SharedPreferences.Editor sharedPreferencesEditor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
sharedPreferencesEditor.putBoolean("getPushNotifications", false);
sharedPreferencesEditor.commit();
}
}
});
}
}
Dank!