开发者

Getting location on first boot FC also and random FC's on some devices

I am getting the current location with the fol开发者_开发百科lowing code but this is causing FC if there is no last known location. Also some devices like Motorola atrix crash if they open this activity.

locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000L, 500.0f, this);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        double lat = location.getLatitude();
        double lng = location.getLongitude();

Is there a better way of getting the current location with out crashing my app?


To stop it from crashing your application, put the code within a try/catch block. Android apps "force close" when an exception occurs and is not caught. If you find that it throws an error because the data is not ready yet, you can try again shortly after the first exception is caught.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜