我試圖做空指針異常
我試圖創建一個PreferenceActivity它承載一個開關按鈕,如果我可以啓用或禁用全球定位系統。佈局和一切工作完美,我添加了代碼後,交換機做了一些我得到一個NullPointerException,這不應該存在我的眼睛。
問題
你能提供給我一個awnser爲什麼這個空指針是有=)有些codesnippets將是有益的。
代碼
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
的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
感謝您的幫助提前
問候
野生動物園
是什麼PrefsActivity.java 35行? – logcat 2012-07-26 09:52:32
使用'getPref()'返回類型'boolean' ,,,,不'布爾' – 2012-07-26 09:52:42
第35行是我在那裏標記爲空指針發生...我還發布了方法setPref和getPref – safari 2012-07-26 09:54:14