我正在尝试做的事情
我正在尝试创建一个承载开关按钮的 PreferenceActivity,如果我可以启用或禁用 gps。布局和一切工作完美,在我添加了 Switch 执行某些操作的代码后,我得到了一个 NullPointerException,它不应该存在于我的眼中。
问题
你能给我一个 awnser 为什么这个 Nullpointer 在那里 =) 一些代码片段会很有帮助。
代码
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();
}
日志猫:
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
提前感谢您的帮助
最好的祝福
苹果浏览器