0

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

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);
}
4

1 回答 1

0

NullPointerException被抛出是因为location你来到这里的可能Location location = mLocationClient.getLastLocation();null. 因此,您可能总是进行空检查。

于 2013-11-09T06:27:19.483 回答