无法启动 activity ComponentInfo - LocationManager

Unable to start activity ComponentInfo - LocationManager

我正在尝试获取纬度和经度以在 Google 地图中显示我的当前位置,但出现此错误:

03-04 18:00:50.785  13625-13625/com.example.thulio.googlemapstest W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40e0c960)
03-04 18:00:50.785  13625-13625/com.example.thulio.googlemapstest E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thulio.googlemapstest/com.example.thulio.googlemapstest.MapPane}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
            at android.app.ActivityThread.access0(ActivityThread.java:157)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5317)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.thulio.googlemapstest.MapPane.getCurrentLocation(MapPane.java:35)
            at com.example.thulio.googlemapstest.MapPane.onCreate(MapPane.java:28)
            at android.app.Activity.performCreate(Activity.java:5326)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
            at android.app.ActivityThread.access0(ActivityThread.java:157)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5317)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)

我只是想获取一次当前位置并显示在地图上。

主要活动:

public class MapPane extends Activity{
    private GoogleMap map;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_pane);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        getCurrentLocation();
    }


    public void getCurrentLocation(){
        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        double longitude = location.getLongitude();
        double latitude = location.getLatitude();

        LatLng USER_LOCATION = new LatLng(longitude, latitude);

        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(USER_LOCATION, 13);
        map.animateCamera(update);

        map.addMarker(new MarkerOptions().position(USER_LOCATION).title("USER LOCATION"));
    }
}

在 AndroidManifest 中:

...
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
...

感谢您的帮助。

您是否也在 AndroidManifest.xml 文件中引用了以下代码段?

<application ...>
   ...
   <uses-library android:name="com.google.android.maps" />
</application>

如果 LocationManager 中指出当前没有已知位置或提供商当前不可用,则这可能 return 无效。您可能不应该依赖它被立即检索,而是将其设置为当它在您的代码中准备就绪时您可以检查它。

这是另一个 example 的人设置管理器并在 try catch 块中进行尝试。尽管您可以随心所欲地进行操作。但不能保证它会立即准备就绪。还需要做其他检查。

你可以看看http://wptrafficanalyzer.in/blog/showing-current-location-in-google-maps-using-api-v2-with-supportmapfragment/。这是一个非常好的教程。