1. Download our Official Android App: Forums for Android!

Apps GPS app seems to crash after phone restart

Discussion in 'Android Development' started by icydash, Aug 17, 2011.

  1. icydash

    icydash Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    12
    Joined:
    Jun 8, 2011

    Jun 8, 2011
    12
    0
    15
    Hey guys, so I have an odd problem. Hopefully I posted this in the right area.

    So like 85% of the time, my GPS code (below) works well. But when I restart the phone, and then run my app, it immediately crashes. Once I run a few apps that use the GPS, like google maps, and Places, then run my app, the app works perfectly again.

    Code (Text):
    1.  
    2. In the main method:
    3.  
    4.        //--------------------------GPS--------------------------
    5.        /* Use the LocationManager class to obtain GPS locations */
    6.        final LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    7.        final LocationListener mlocListener = new MyLocationListener();
    8.        //---------------------------------------------------------------------
    9.  
    10.  
    11. ...later in the code in the main method:
    12.  
    13.            Location location = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    14.        String updateGPS = location.getLatitude() + "x" + location.getLongitude();
    15.        
    16.  
    17. and of course my MyLocationListener class:
    18.  
    19.  public class MyLocationListener implements LocationListener
    20.     {
    21.     @Override
    22.         public void onLocationChanged(Location loc)
    23.         {
    24.             loc.getLatitude();
    25.             loc.getLongitude();
    26.             //String Text = "My current location is: " + "\nLatitude = " + loc.getLatitude() + "\nLongitude = " + loc.getLongitude();
    27.             //Toast.makeText( getApplicationContext(),"location changed", Toast.LENGTH_SHORT).show();
    28.         }
    29.         @Override
    30.         public void onProviderDisabled(String provider)
    31.         {
    32.             Toast.makeText( getApplicationContext(),"GPS Disabled",Toast.LENGTH_SHORT ).show();
    33.         }
    34.         @Override
    35.         public void onProviderEnabled(String provider)
    36.         {
    37.             Toast.makeText( getApplicationContext(), "GPS Enabled",Toast.LENGTH_SHORT).show();
    38.         }
    39.         @Override
    40.         public void onStatusChanged(String provider, int status, Bundle extras)
    41.         {
    42.             Toast.makeText( getApplicationContext(), "status " + status,Toast.LENGTH_SHORT).show();
    43.         }
    44.     }/* End of Class MyLocationListener */
    45.  
    The line that seems to be the problem is "String updateGPS = location.getLatitude() + "x" + location.getLongitude();"

    It seems like the restart of the phone is clearing the GPS data, and when my app tries to run location.getLatitude() and location.getLongitude(), it doesn't get anything, and crashes.

    ...but once I run some other apps and they store some GPS data in the phone, and then MY app tries to getLatitude and getLongitude, since something is stored in the phone, the app can once again work properly.

    Is there a way for me to fix this bug? Like to check if getLatitude and getLongitude return null values before I do the "String updateGPS ..." line?
     

    Advertisement

  2. icydash

    icydash Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    12
    Joined:
    Jun 8, 2011

    Jun 8, 2011
    12
    0
    15
    Nevermind, got it. Had to use: if (location != null) {} ... I had tried something similar but couldn't get it to work, but that worked.
     

Share This Page

Loading...