简体   繁体   中英

Android Google Map V2 - Find my location error

I'm trying to retrieve the device's location but I faced several problems:

My 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

My MainActivity.java code:

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"));

    }
}

I also tried to use the codes on google.developer.com however its too confusing can't run really understand most of them and got errors when I tried to run them. What should I do to solve this problem? Comments are appreciated. :)

The call to locationManager.getLastKnownLocation(provider) is not blocking. So there is no guarantee that you will get the location immediately. With that said your location object is null.

To prevent that you should use the LocationListener onLocationChanged() callback to be sure to get a valid location. This callback returns a location as soon as it is available.

Another fact is if your app never had a last location you will not get one. You first have to call requestForLocationUpdates().

Edit: In the docs it is described in detail.

You can use it like this:

requestLocationUpdates (provider, minTimeForUpdates, minDistanceForUpdates, yourLocationListener);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM