我从以下代码行中得到一个空指针异常:
double longitude = location.getLongitude();
我在弄清楚问题所在时遇到了问题。
LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, waypointActivity);
//LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
String locLat = String.valueOf(latitude)+","+String.valueOf(longitude);
这是 logcat 输出:
04-30 10:20:54.988: E/AndroidRuntime(1827): FATAL EXCEPTION: main
04-30 10:20:54.988: E/AndroidRuntime(1827): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.LoadedApk.makeApplication(LoadedApk.java:504)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4364)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.ActivityThread.access$1300(ActivityThread.java:141)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.os.Looper.loop(Looper.java:137)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-30 10:20:54.988: E/AndroidRuntime(1827): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 10:20:54.988: E/AndroidRuntime(1827): at java.lang.reflect.Method.invoke(Method.java:511)
04-30 10:20:54.988: E/AndroidRuntime(1827): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-30 10:20:54.988: E/AndroidRuntime(1827): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-30 10:20:54.988: E/AndroidRuntime(1827): at dalvik.system.NativeStart.main(Native Method)
04-30 10:20:54.988: E/AndroidRuntime(1827): Caused by: java.lang.NullPointerException
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:379)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.LoadedApk.getClassLoader(LoadedApk.java:322)
04-30 10:20:54.988: E/AndroidRuntime(1827): at android.app.LoadedApk.makeApplication(LoadedApk.java:496)
04-30 10:20:54.988: E/AndroidRuntime(1827): ... 11 more
04-30 10:21:04.048: E/Trace(1863): error opening trace file: No such file or directory (2)
我不明白为什么它认为我的位置变量为空。任何建议,将不胜感激。