NullPointerException in a GPS Android Application

I have the following very simple class that searches for the last known user coordinates and then prints one of them. But I get loc as a null variable. The application does not read data into the loc variable.

    import android.app.Activity;
    import android.content.Context;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.widget.Toast;

    public class Navigation extends Activity
    {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /* Use the LocationManager class to obtain GPS locations */
        try
        {
            LocationManager mlocManager = 
              (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            Toast.makeText(getBaseContext(), 
                  "Provider Enable Status : " + mlocManager.isProviderEnabled(                       
                   mlocManager.getProvider("gps").getName()),     
                   Toast.LENGTH_LONG).show();

            Location loc = mlocManager.getLastKnownLocation("gps");
            Toast.makeText( getApplicationContext(), "" + 
                loc.getLatitude(),  Toast.LENGTH_LONG).show();
            Toast.makeText( getApplicationContext(), "" + loc,
                Toast.LENGTH_LONG).show();
         } catch (Exception e) {
           Toast.makeText(getBaseContext(), e.getMessage() + 
             e.toString(), Toast.LENGTH_LONG).show();
         }
      }
   }

I added the following listener to the code:

        LocationListener mlocListener = new MyLocationListener();

        mlocManager.requestLocationUpdates( mlocManager.getProvider("gps").getName(), 0, 0, mlocListener);

in the oncreate function. And the following as a class in

public class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location loc) {
        Toast.makeText( getApplicationContext(),
            "Hi, location changed. :)", Toast.LENGTH_LONG).show();
        try {
        loc.getLatitude();
        loc.getLongitude();
        String Text = "My current location is: " +
                " Latitude = " + loc.getLatitude() +
                " Longitude = " + loc.getLongitude();
        Toast.makeText(getApplicationContext(),
                Text, Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), 
                e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
}

And after starting the window, I do the following: go to DDMS in Eclipse and set the altitude / longitude values ​​and click on submit.

However, it does not run the toast command in the function.

+3
source share
4 answers

, , Thats . onResume(), :

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f,this);
    super.onResume();
}

mlocManager , (), ( , onCreate).

, , . , GPS. DDMS. latitiude .

, !

0

, " null" .

Android, , null GPS , . , loc !=null, . .

, .

+1

... , , . . GPS...

+1

, , . , , .

, , , getApplication() getBaseContext(). this .

Toast, ""+loc , ""+loc.getLongitude().

- , , .

private Context ctx = null;

public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    // some code

    ctx = getApplication();

    // some more code

}

// your other class in the same Java file (inner class)

public class MyOtherClass {
    // a construtctor probably
    // some usefull methods
    Toast.makeText(ctx, "My message", 5000).show();
}
+1

Source: https://habr.com/ru/post/1774500/


All Articles