找到我当前的位置时出错

时间:2013-11-09 05:49:59

标签: android google-maps location latitude-longitude android-maps-v2

我正在开发应用程序以获取我在地图上的当前位置并显示最近的位置。 首先我的应用程序正在运行,但相机进入固定Latlng(我把值),但当我更改代码,使相机在我当前的位置..我的手机关闭。 这是我的logcat:

11-09 08:38:51.010: E/AndroidRuntime(1641): FATAL EXCEPTION: main
11-09 08:38:51.010: E/AndroidRuntime(1641): java.lang.NullPointerException
11-09 08:38:51.010: E/AndroidRuntime(1641):     at     com.example.buttontest.NearbyActivity.onConnected(NearbyActivity.java:219)
11-09 08:38:51.010: E/AndroidRuntime(1641):     at com.google.android.gms.internal.u.v(Unknown Source)
11-09 08:38:51.010: E/AndroidRuntime(1641):     at com.google.android.gms.internal.u$f.a(Unknown Source)
11-09 08:38:51.010: E/AndroidRuntime(1641):     at com.google.android.gms.internal.u$f.a(Unknown Source)
11-09 08:38:51.010: E/AndroidRuntime(1641):     at com.google.android.gms.internal.u$b.A(Unknown Source)
 11-09 08:38:51.010: E/AndroidRuntime(1641):    at com.google.android.gms.internal.u$a.handleMessage(Unknown Source)
11-09 08:38:51.010: E/AndroidRuntime(1641):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 08:38:51.010: E/AndroidRuntime(1641):     at android.os.Looper.loop(Looper.java:137)
11-09 08:38:51.010: E/AndroidRuntime(1641):     at android.app.ActivityThread.main(ActivityThread.java:4898)
11-09 08:38:51.010: E/AndroidRuntime(1641):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 08:38:51.010: E/AndroidRuntime(1641):     at java.lang.reflect.Method.invoke(Method.java:511)
11-09 08:38:51.010: E/AndroidRuntime(1641):     at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
11-09 08:38:51.010: E/AndroidRuntime(1641):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
11-09 08:38:51.010: E/AndroidRuntime(1641):     at dalvik.system.NativeStart.main(Native Method)

这是错误的地方

@Override
public void onConnected(Bundle dataBundle) {
    // Display the connection status
    Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
    Location location = mLocationClient.getLastLocation();
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 17);
    map.animateCamera(cameraUpdate);
}

1 个答案:

答案 0 :(得分:0)

NullPointerException被抛出,因为您location Location location = mLocationClient.getLastLocation(); null可能是{{1}}。因此,您可能总是进行空检查。