当我尝试获取如下所示的最后一个已知位置时,应用程序将停止工作并崩溃。当我省略该行时,该应用程序工作正常。 "位置" variable是全局定义的私有变量。请让我知道我做错了什么。
JavaCode:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gpstest00);
tvLatitudeValue = (TextView) findViewById(R.id.tvLatitudeValue);
tvLongitudeValue = (TextView) findViewById(R.id.tvLongitudeValue);
locMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
gpsEnable = locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsEnable)
{
showGPSDialogueBox();
}
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1,
this.locationListener);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
logcat的:
04-16 15:53:50.016: E/AndroidRuntime(1755): FATAL EXCEPTION: main
04-16 15:53:50.016: E/AndroidRuntime(1755): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gpstest00/com.example.gpstest00.GPSTest00}: java.lang.NullPointerException
04-16 15:53:50.016: E/AndroidRuntime(1755): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
04-16 15:53:50.016: E/AndroidRuntime(1755): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
04-16 15:53:50.016: E/AndroidRuntime(1755): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-16 15:53:50.016: E/AndroidRuntime(1755): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
04-16 15:53:50.016: E/AndroidRuntime(1755): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 15:53:50.016: E/AndroidRuntime(1755): at android.os.Looper.loop(Looper.java:137)
04-16 15:53:50.016: E/AndroidRuntime(1755): at android.app.ActivityThread.main(ActivityThread.java:5103)
04-16 15:53:50.016: E/AndroidRuntime(1755): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 15:53:50.016: E/AndroidRuntime(1755): at java.lang.reflect.Method.invoke(Method.java:525)
04-16 15:53:50.016: E/AndroidRuntime(1755): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-16 15:53:50.016: E/AndroidRuntime(1755): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-16 15:53:50.016: E/AndroidRuntime(1755): at dalvik.system.NativeStart.main(Native Method)
04-16 15:53:50.016: E/AndroidRuntime(1755): Caused by: java.lang.NullPointerException
04-16 15:53:50.016: E/AndroidRuntime(1755): at com.example.gpstest00.GPSTest00.onCreate(GPSTest00.java:47)
04-16 15:53:50.016: E/AndroidRuntime(1755): at android.app.Activity.performCreate(Activity.java:5133)
04-16 15:53:50.016: E/AndroidRuntime(1755): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-16 15:53:50.016: E/AndroidRuntime(1755): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
04-16 15:53:50.016: E/AndroidRuntime(1755): ... 11 more