CompoundButton(Switch)上的NullPointer异常

时间:2012-07-26 09:50:46

标签: android android-widget switch-statement

我正在尝试做什么


我正在尝试创建一个托管Switch按钮的PreferenceActivity,如果可以启用或禁用gps。布局和一切都很完美,在我添加了Switch执行某些操作的代码后,我得到了一个N​​ullPointerException,这在我看来不应该存在。

问题


你能为我提供一个更好的理由,为什么这个Nullpointer在那里=)一些codenippets会有所帮助。

代码


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和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

感谢您提前的帮助

最好的问候

狩猎

2 个答案:

答案 0 :(得分:6)

添加此声明

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

// main是一个xml布局,您已声明 Rid.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);
    }

答案 1 :(得分:3)

您正在访问此视图enable_gps = (Switch) findViewById(R.id.switch1);而未调用setContentView();,因此此处enable_gps为null,您尝试调用enable_gps.setChecked(false);因此导致NPE ..