我将此代码放在针对API级别17的应用中的onCreate方法中:
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// connect to the GPS location service
Location loc = lm.getLastKnownLocation(lm.NETWORK_PROVIDER);
Toast.makeText(MainActivity.this,
"Latitude -> "+Double.toString(loc.getLatitude()) +
" Longitude is -> "+Double.toString(loc.getLongitude()),
Toast.LENGTH_LONG).show();
但它总是给我力量接近。我有Internet,FineLocation权限。
08-14 00:33:18.057: E/AndroidRuntime(330): FATAL EXCEPTION: main
08-14 00:33:18.057: E/AndroidRuntime(330): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.b/com.example.b.MainActivity}: java.lang.NullPointerException
08-14 00:33:18.057: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-14 00:33:18.057: E/AndroidRuntime(330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-14 00:33:18.057: E/AndroidRuntime(330): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-14 00:33:18.057: E/AndroidRuntime(330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-14 00:33:18.057: E/AndroidRuntime(330): at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 00:33:18.057: E/AndroidRuntime(330): at android.os.Looper.loop(Looper.java:123)
08-14 00:33:18.057: E/AndroidRuntime(330): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-14 00:33:18.057: E/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 00:33:18.057: E/AndroidRuntime(330): at java.lang.reflect.Method.invoke(Method.java:521)
08-14 00:33:18.057: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-14 00:33:18.057: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-14 00:33:18.057: E/AndroidRuntime(330): at dalvik.system.NativeStart.main(Native Method)
08-14 00:33:18.057: E/AndroidRuntime(330): Caused by: java.lang.NullPointerException
08-14 00:33:18.057: E/AndroidRuntime(330): at com.example.b.MainActivity.onCreate(MainActivity.java:23)
08-14 00:33:18.057: E/AndroidRuntime(330): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-14 00:33:18.057: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-14 00:33:18.057: E/AndroidRuntime(330): ... 11 more
答案 0 :(得分:0)
我猜你的MainActivity第23行是这样的:
Toast.makeText(MainActivity.this, "Latitude -> "+Double.toString(loc.getLatitude())
在此行之后进行空检查
Location loc = lm.getLastKnownLocation(lm.NETWORK_PROVIDER);
网络提供商可能没有最后已知的位置,并且loc为null。
试试这个:
LocationManager lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
// connect to the GPS location service
Location loc = lm.getLastKnownLocation(lm.NETWORK_PROVIDER);
//Try to get it from the GPS_PROVIDER
if (loc == null)
{
loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);
}
if (loc != null){
Toast.makeText(MainActivity.this, "Latitude -> "+Double.toString(loc.getLatitude())
+" Longitude is -> "+Double.toString(loc.getLongitude()), Toast.LENGTH_LONG).show();
}else{
//If it's still null there is no other way to get the last location. Display a message if you like.
Toast.makeText(MainActivity.this, "No location found", Toast.LENGTH_LONG).show();
}