0

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

我的日志猫:

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 上的代码,但是它太混乱了,无法真正理解其中的大多数,并且在我尝试运行它们时出现错误。我应该怎么做才能解决这个问题?评论表示赞赏。:)

4

1 回答 1

0

调用locationManager.getLastKnownLocation(provider)不会阻塞。因此,不能保证您会立即获得该位置。话虽如此,您的位置对象为空。

为防止发生这种情况,您应该使用LocationListener onLocationChanged() 回调来确保获得有效的位置。此回调会在位置可用时立即返回。

另一个事实是,如果您的应用程序从来没有最后一个位置,您将不会得到一个。您首先必须调用 requestForLocationUpdates()。

编辑:在文档中对其进行了详细描述。

你可以像这样使用它:

requestLocationUpdates (provider, minTimeForUpdates, minDistanceForUpdates, yourLocationListener);

于 2013-07-21T15:13:02.903 回答