Android Google Map V2 - 查找我的位置错误

时间:2013-07-21 14:32:43

标签: android google-maps location google-api

我正在尝试检索设备的位置,但我遇到了几个问题:

我的LogCat:

07-21 22:23:34.072: E/AndroidRuntime(11634): FATAL EXCEPTION: main
07-21 22:23:34.072: E/AndroidRuntime(11634): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Test.projecttest/com.Test.projecttest.MainActivity}: java.lang.NullPointerException
07-21 22:23:34.072: E/AndroidRuntime(11634):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
07-21 22:23:34.072: E/AndroidRuntime(11634):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
07-21 22:23:34.072: E/AndroidRuntime(11634):    at android.app.ActivityThread.access$700(ActivityThread.java:134)
07-21 22:23:34.072: E/AndroidRuntime(11634):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217)
07-21 22:23:34.072: E/AndroidRuntime(11634):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 22:23:34.072: E/AndroidRuntime(11634):    at android.os.Looper.loop(Looper.java:137)
07-21 22:23:34.072: E/AndroidRuntime(11634):    at android.app.ActivityThread.main(ActivityThread.java:4856)
07-21 22:23:34.072: E/AndroidRuntime(11634):    at java.lang.reflect.Method.invokeNative(Native Method)
07-21 22:23:34.072: E/AndroidRuntime(11634):    at java.lang.reflect.Method.invoke(Method.java:511)
07-21 22:23:34.072: E/AndroidRuntime(11634):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
07-21 22:23:34.072: E/AndroidRuntime(11634):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
07-21 22:23:34.072: E/AndroidRuntime(11634):    at dalvik.system.NativeStart.main(Native Method)
07-21 22:23:34.072: E/AndroidRuntime(11634): Caused by: java.lang.NullPointerException
07-21 22:23:34.072: E/AndroidRuntime(11634):    at com.Test.projecttest.MainActivity.setUpMap(MainActivity.java:82)
07-21 22:23:34.072: E/AndroidRuntime(11634):    at com.Test.projecttest.MainActivity.onCreate(MainActivity.java:71)
07-21 22:23:34.072: E/AndroidRuntime(11634):    at android.app.Activity.performCreate(Activity.java:5047)
07-21 22:23:34.072: E/AndroidRuntime(11634):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-21 22:23:34.072: E/AndroidRuntime(11634):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
07-21 22:23:34.072: E/AndroidRuntime(11634):    ... 11 more

我的MainActivity.java代码:

public class MainActivity extends FragmentActivity implements OnClickListener, LocationListener {

private GoogleMap map;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SupportMapFragment fmap = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        map = fmap.getMap();
        setUpMap();
        }


    private void setUpMap() {
        map.setMyLocationEnabled(true);
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, true);
        Location myLocation = locationManager.getLastKnownLocation(provider);
        map.setMapType(map.MAP_TYPE_HYBRID);
        double latitude = myLocation.getLatitude();
        double longitude = myLocation.getLongitude();
        LatLng latLng = new LatLng(latitude, longitude);
        map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        map.animateCamera(CameraUpdateFactory.zoomTo(20));
        map.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("I'M HERE"));

    }
}

我还试图在google.developer.com上使用这些代码,但是它太混乱了,无法运行真正了解大多数代码并在我尝试运行它们时出错。我该怎么做才能解决这个问题?评论表示赞赏。 :)

1 个答案:

答案 0 :(得分:0)

locationManager.getLastKnownLocation(provider)的调用没有阻止。因此,无法保证您将立即获得该位置。据说你的位置对象为空。

为防止您使用LocationListener onLocationChanged()回调,请务必获取有效位置。该回调一旦可用就会返回一个位置。

另一个事实是,如果您的应用从未有过最后一个位置,那么您将无法获得。首先必须调用requestForLocationUpdates()。

编辑:在docs中详细介绍。

你可以像这样使用它:

requestLocationUpdates(provider,minTimeForUpdates,minDistanceForUpdates,yourLocationListener);