Raven Ryuuzaki
Raven Ryuuzaki

Reputation: 11

Google Map current location crash

When I try to open my application in my phone it always say "Unfortunately the Application has stopped" Can someone help me with this problem. Here is my code

I'm getting and error here setUpMapIfNeeded();

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

and I'm getting and error here if (mMap == null) {

     private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

    private void setUpMap() { 
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker").snippet("Snippet")); 

     mMap.setMyLocationEnabled(true); 

     LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);   
    Criteria criteria = new Criteria(); 
    String provider = locationManager.getBestProvider(criteria, true);   
    Location myLocation = locationManager.getLastKnownLocation(provider);  
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);   
     double latitude = myLocation.getLatitude();   
     double longitude = myLocation.getLongitude(); 
     LatLng latLng = new LatLng(latitude, longitude); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    mMap.animateCamera(CameraUpdateFactory.zoomTo(14)); 
    mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!").snippet("Consider yourself located")); 
} 

Logcat

 08-20 18:04:40.626  10733-10733/com.example.rontaku.mapsdemo E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.rontaku.mapsdemo, PID: 10733
java.lang.RuntimeException: Unable to resume activity {com.example.rontaku.mapsdemo/com.example.rontaku.mapsdemo.MapsActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3076)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3105)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.access$900(ActivityThread.java:175)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5602)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.rontaku.mapsdemo.MapsActivity.setUpMapIfNeeded(MapsActivity.java:55)
        at com.example.rontaku.mapsdemo.MapsActivity.onResume(MapsActivity.java:47)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1198)
        at android.app.Activity.performResume(Activity.java:5530)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3066)

           

Upvotes: 1

Views: 406

Answers (2)

Faiyaz
Faiyaz

Reputation: 572

I think you should add these permissions to you app manifest file.

<uses-permission android:name="android.permission.INTERNET"
      android:name="android.permission.WRITE_EXTERNAL_STORAGE"
      android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"
      android:name="android.permission.ACCESS_COARSE_LOCATION"
      android:name="android.permission.ACCESS_FINE_LOCATION"
      android:name="your_package_name.MAPS_RECEIVE" />

EDIT

private void setUpMap() { 
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker").snippet("Snippet")); 

     mMap.setMyLocationEnabled(true); 

     LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);   
    Criteria criteria = new Criteria(); 
    String provider = locationManager.getBestProvider(criteria, true);   
    Location myLocation = locationManager.getLastKnownLocation(provider);  
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
if (myLocation != null)  {
     double latitude = myLocation.getLatitude();   
     double longitude = myLocation.getLongitude(); 
     LatLng latLng = new LatLng(latitude, longitude); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    mMap.animateCamera(CameraUpdateFactory.zoomTo(14)); 
    mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!").snippet("Consider yourself located")); 
} 

Upvotes: 1

N Dorigatti
N Dorigatti

Reputation: 3540

you are trying to read last known location assuming there is one, but that method can give you null if there is no last known location, just change to:

private void setUpMap() { 
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker").snippet("Snippet")); 
    mMap.setMyLocationEnabled(true); 
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);   
    Criteria criteria = new Criteria(); 
    String provider = locationManager.getBestProvider(criteria, true);   
    Location myLocation = locationManager.getLastKnownLocation(provider);  

    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);   
    if (null != myLocation){
        double latitude = myLocation.getLatitude();   
        double longitude = myLocation.getLongitude(); 
        LatLng latLng = new LatLng(latitude, longitude); 
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
        mMap.animateCamera(CameraUpdateFactory.zoomTo(14)); 
        mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!").snippet("Consider yourself located"));
    }  
} 

Upvotes: 0

Related Questions