0

I develop app with a Map and published the app to market . now, i have 2 crashes & ANRs from two users of my app . but to me i'ts work perfect! and i no have a clue what the problem?! :( here is my code and the error they get:

public class MapCurrentPlace2 implements LocationListener {

public Context context;
double latitude = 0;
double longitude = 0;
String placeName = "";

// then use below code
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

Location location = null;
 boolean isGPSEnabled;
 boolean isNetworkEnabled;
 private boolean canGetLocation;


public MapCurrentPlace2(Context context) {
    super();
    this.context = context;
}


public Location getLocation() {

    try {
        LocationManager locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);

        // getting GPS status
         isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
         isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);


        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
            } else {
                    this.canGetLocation = true;
                    if (isNetworkEnabled) {
                        locationManager.requestLocationUpdates(  LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, (android.location.LocationListener) this);
                        Log.d("Network", "Network Enabled");

                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, (android.location.LocationListener) this);



                    Log.d("GPS", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}


@Override
public void onLocationChanged(Location arg0) {

}


public double getLat(){
    location = getLocation();
    latitude = location.getLatitude();
    return latitude;
}


public double getLng(){
    location = getLocation();
    longitude = location.getLatitude();
    return latitude;
}

the first user error:

Android version: Android 4.21 Device: Lg g2

java.lang.NullPointerException
at com.bi.workclock.tools.MapCurrentPlace.getLatitude(MapCurrentPlace.java:42)
at com.bi.workclock.MainActivity.startTimer(MainActivity.java:269)
at com.bi.workclock.MainActivity.onLongClick(MainActivity.java:786)
at android.view.View.performLongClick(View.java:4376)
at android.view.View$CheckForLongPress.run(View.java:17613)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5171)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
at dalvik.system.NativeStart.main(Native Method)

and the sec user error: Android version: Android 4.31 Device: Galaxy S3 (m0)

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.bi.workclock.tools.MapCurrentPlace.getPlaceName(MapCurrentPlace.java:74)
at com.bi.workclock.MainActivity.startTimer(MainActivity.java:271)
at com.bi.workclock.MainActivity.onLongClick(MainActivity.java:786)
at android.view.View.performLongClick(View.java:4511)
at android.view.View$CheckForLongPress.run(View.java:18758)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)

Here is my code version2 ...**its a good way ?!:**

public class MapCurrentPlace {

    public Context context;
    double latitude = 0;
    double longitude = 0;
    String placeName = "";



    public MapCurrentPlace(Context context) {
        super();
        this.context = context;
    }




    public double getLatitude() throws IOException {
        try {
            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            Criteria criteria = new Criteria();
            String provider = locationManager.getBestProvider(criteria, true);
            Location location = locationManager.getLastKnownLocation(provider);

            if(location != null){
                latitude = location.getLatitude();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return latitude;
    }

    public double getLongitude() throws IOException {
        try {
            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            Criteria criteria = new Criteria();
            String provider = locationManager.getBestProvider(criteria, true);
            Location location = locationManager.getLastKnownLocation(provider);
            if(location != null){
                longitude = location.getLongitude();    
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return longitude;
    }


    public String getPlaceName() throws IOException {
        try {
            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            Criteria criteria = new Criteria();
            String provider = locationManager.getBestProvider(criteria, true);
            Location location = locationManager.getLastKnownLocation(provider);

            latitude = location.getLatitude();
            longitude = location.getLongitude();

            //Get location info
            Geocoder gc = new Geocoder(context);
            if(location != null && gc !=null){
                List<Address> list = gc.getFromLocation(latitude, longitude, 1);
                if(list.size()>0){
                    String city = list.get(0).getLocality();
                    String street = list.get(0).getAddressLine(0);
                    placeName = city+", "+street+"";        
                }


            }else{
                placeName = "";
            }


        } catch (Exception e) {
            e.printStackTrace();
            placeName = "";
        }



        return placeName;
    }

Thank you all i love this site!

3 Answers3

1

Because of your Geocoder class sometimes return null value thats why your app is crashing use Google place Api to find the address through your lat long and follow the link how to use Using Google Places API

Community
  • 1
  • 1
Ravind Maurya
  • 2,609
  • 1
  • 20
  • 27
0

The getLastKnownLocation() this returns the last known location of user if user not opened maps or re-install the app it will not have any last known location. So it will result in Null pointer exception.

try below code:-

class abc implements LocationListener // then use below code
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network Enabled");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}
duggu
  • 37,191
  • 12
  • 114
  • 111
  • i try your suggest but what i need to do in line:ocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this); –  May 20 '14 at 09:25
  • @user3453502 not understand what you want to say? – duggu May 20 '14 at 09:26
  • I copied and pasted your code but the line : cpMIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES " is (cpMIN_TIME_BW_UPDATES cannot be resolved to a variable) –  May 20 '14 at 09:27
  • @user3453502 see updated code or you have to implement LocationListener ! – duggu May 20 '14 at 09:31
  • ok thanks but i'm implements LocationListener . but what the value float value i need to write in MIN_TIME_BW_UPDATES and MIN_DISTANCE_CHANGE_FOR_UPDATES ?! –  May 20 '14 at 09:39
  • Sorry i didnt see it..:) ok i'm try to run this code –  May 20 '14 at 09:50
  • @user3453502 see on top where code start (add these code class abc implements LocationListener ) – duggu May 20 '14 at 09:50
  • @user3453502 now whats your problem dude? – duggu May 20 '14 at 10:03
  • I dont know its my first time and I just wanna learn how to do this properly –  May 20 '14 at 10:13
  • i get a error : java.lang.ClassCastExeption com.bib.work.tools.Abc cannot be cast to android.location.LocationListener –  May 20 '14 at 12:11
  • @user3453502 i donot know what you trying now so will you show will you code ? – duggu May 20 '14 at 12:28
0

I think you are missing a few error detection code lines. Add an if to check if the arraylist.length is > than 0. That should solve the galaxy s3 problem. And for the g2 Another if for the latitude but also the longitude aswell to check if the values are not null then do the code you are doing.

You always need these kind of checkups so you dont end up with FC closes and errors just in case like this happens.