简体   繁体   中英

Attempt to invoke virtual method 'void android.widget.Switch.setChecked(boolean)' on a null object reference

I have created a settings activity and I want to save the Switch Button with SharedPreferences, but I get a error (see title) when I start the Activity:

The error is in line 13.

The 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();

                }
            }
        });

    }

}

Thanks!

All you need to do is swap these two lines (change their order):

switchPushNotifications.setChecked(sharedPreferences.getBoolean("getPushNotifications", true));

switchPushNotifications = (Switch) findViewById(R.id.switchPush);

You should first initialize it, and then use it. This way you're trying to access a method on something that's still null and thus the NullPointerException .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Enabling button from another activity - Attempt to invoke virtual method 'void android.widget.Button.setEnabled(boolean)' on a null object reference Encountering a Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference Android error: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference? Android Studio: Attempt to invoke virtual method 'void android.widget.ImageButton.setImageResource(int)' on a null object reference Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference I got Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference Attempt to invoke virtual method 'void android.widget.Editor$SelectionModifierCursorController.hide()' on a null object reference Attempt to invoke virtual method 'void android.widget.Editor$InsertionPointCursorController.show()' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM