Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointer Exception on a CompoundButton (Switch)

What I'm trying to do


I'm trying to create a PreferenceActivity which hosts a Switch-Button, if which i can enable or disable the gps. The Layout and everything works perfectly, after I added the code that the Switch does something I get a NullPointerException which should not exist in my eyes.

Question


Could you provide me an awnser why this Nullpointer is there =) Some codesnippets would be helpfull.

Code


PrefsActivity:

private Switch enable_gps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);

        enable_gps = (Switch) findViewById(R.id.switch1);

        ActionBar actionBar = this.getActionBar();
        // So kann android.R.id.home benützt werden
        actionBar.setDisplayHomeAsUpEnabled(true);

        if (getPref() == false) {  <--- Here I get a Nullpointer
            // enable_gps.setEnabled(false);
            enable_gps.setChecked(false);
        } else {
            enable_gps.setChecked(true);
        }

        enable_gps.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if (getPref() == false) {

                    GPSService.actionStart(getApplicationContext());
                    setPref(true);

                } else if (getPref() == true) {

                    GPSService.actionStop(getApplicationContext());
                    setPref(false);

                }
            }
        });

    }

getPref and setPref

//Um zu prüfen ob on oder off
    private boolean getPref() {
        boolean isStarted;
        // SharedPreferences instanzieren
        SharedPreferences mPrefs = this.getSharedPreferences(LOG_TAG, 0);
        return isStarted = mPrefs.getBoolean(PREF_STARTED, false);
    }

    //Um zu setzen ob on oder off
    private void setPref(boolean started) {
        SharedPreferences settings = this.getSharedPreferences(LOG_TAG, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(PREF_STARTED, started);
        editor.commit();
    }

Logcat:

07-26 09:47:02.991: E/AndroidRuntime(21729): FATAL EXCEPTION: main
07-26 09:47:02.991: E/AndroidRuntime(21729): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.ivocore/de.ivocore.preferences.PrefsActivity}: java.lang.NullPointerException
07-26 09:47:02.991: E/AndroidRuntime(21729):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-26 09:47:02.991: E/AndroidRuntime(21729):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-26 09:47:02.991: E/AndroidRuntime(21729):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-26 09:47:02.991: E/AndroidRuntime(21729):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-26 09:47:02.991: E/AndroidRuntime(21729):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 09:47:02.991: E/AndroidRuntime(21729):    at android.os.Looper.loop(Looper.java:137)
07-26 09:47:02.991: E/AndroidRuntime(21729):    at android.app.ActivityThread.main(ActivityThread.java:4745)
07-26 09:47:02.991: E/AndroidRuntime(21729):    at java.lang.reflect.Method.invokeNative(Native Method)
07-26 09:47:02.991: E/AndroidRuntime(21729):    at java.lang.reflect.Method.invoke(Method.java:511)
07-26 09:47:02.991: E/AndroidRuntime(21729):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-26 09:47:02.991: E/AndroidRuntime(21729):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-26 09:47:02.991: E/AndroidRuntime(21729):    at dalvik.system.NativeStart.main(Native Method)
07-26 09:47:02.991: E/AndroidRuntime(21729): Caused by: java.lang.NullPointerException
07-26 09:47:02.991: E/AndroidRuntime(21729):    at de.ivocore.preferences.PrefsActivity.onCreate(PrefsActivity.java:35)
07-26 09:47:02.991: E/AndroidRuntime(21729):    at android.app.Activity.performCreate(Activity.java:5008)
07-26 09:47:02.991: E/AndroidRuntime(21729):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-26 09:47:02.991: E/AndroidRuntime(21729):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-26 09:47:02.991: E/AndroidRuntime(21729):    ... 11 more

thanks for your help in advance

best regards

safari

like image 999
safari Avatar asked Jul 12 '26 14:07

safari


2 Answers

add this statement

setContentView(R.layout.main); <-----

// main is an xml layour where you have declared R.id.switch1

enable_gps = (Switch) findViewById(R.id.switch1);
                                   ^^^^^^^^^^^^

private boolean getPref() {
        boolean isStarted=false;<------
        // SharedPreferences instanzieren
        SharedPreferences mPrefs = this.getSharedPreferences(LOG_TAG, 0);
        return isStarted = mPrefs.getBoolean(PREF_STARTED, false);
    }
like image 131
MAC Avatar answered Jul 15 '26 04:07

MAC


You are accessing this view enable_gps = (Switch) findViewById(R.id.switch1); without calling setContentView(); so here enable_gps is null , and you are trying to call enable_gps.setChecked(false); hence it is causing NPE..

like image 21
AAnkit Avatar answered Jul 15 '26 05:07

AAnkit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!