0

my app crashes when the location service is turned off. Its gives NullPointerException. I have added the permissions of coarse and fine location. Now how do i handle this? I want to handle this exception and give the user a toast that "location service is off. please turn on the location service". If anyone knows,please help. below is my code.

package com.bddevs.ddbllocator;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends ActionBarActivity {
private GoogleMap googleMap;;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setUpMapIfNeeded();

}

private void setUpMapIfNeeded() {
    if (googleMap == null) {
        googleMap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
        if (googleMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    googleMap.setMyLocationEnabled(true);
    LocationManager locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);
    String provider = LocationManager.NETWORK_PROVIDER;

    Location myLocation = locationmanager.getLastKnownLocation(provider);
    googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
    double latitude = myLocation.getLatitude();
    double longitude = myLocation.getLongitude();
    LatLng latlng = new LatLng(latitude, longitude);
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

Please help if you can.

Hasib Hasan
  • 93
  • 1
  • 2
  • 10
  • 1) what logcat says? 2) it tells you when you debug 3) where checking these coordinates 4) the map works well with fixed coordinates – phipex Mar 29 '14 at 14:04
  • 1. The logcat says it is anullpointerexception 2. Its occurs when i try to run the app 3. The coordinates is fetching the user LatLngs through location service 4. I cant use fixed coordinates here as it fetches the user location LatLngs – Hasib Hasan Mar 29 '14 at 14:56
  • I think the problem you have is that the service location not giving you this well coordinates, the map is damaged when you try to focus on a coordinates are bad, you need to use your code with fixed coordinates to test that the map work properly, and also you need to take out the log the coordinates service delivery location you, I recommend that you separate the code and use try-catch to keep running the app and you can view the log for errors – phipex Mar 29 '14 at 20:21
  • The map works perefectly fine and the app is also running sound and fine when the LOCATION_SERVICE is ENABLED. – Hasib Hasan Mar 29 '14 at 21:14
  • sorry, had misunderstood your problem, the truth and there are many threads that talk about it resolved, only you had to do more research on the above questions, you just have to call isProviderEnabled the LocationManager object http://stackoverflow.com/questions/10154937/isproviderenabledlocationmanager-network-provider-return-false – phipex Mar 31 '14 at 14:33

0 Answers0