-1

Im trying to mail a user my long and lat and im getting nullPointerException:

Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference

I guess that the method getLastKnownLocation() returns null becuase the emulator start with no position?

if so how can i do this in another way?

Here is my code:

public class MainActivity extends Activity {


Button btnSend;
TextView txtview;
String email = "superman@hotmail.com";
String position;


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



    txtview = (TextView) findViewById(R.id.textView);
    btnSend = (Button) findViewById(R.id.button);

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    Double lat = location.getLatitude();
    Double lng = location.getLongitude();

     position = Double.toString(lat + lng);



    btnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



            String[] TO = {"batman@hotmail.com"};

            Intent sendMypos = new Intent(Intent.ACTION_SEND);
            sendMypos.putExtra(Intent.EXTRA_EMAIL,TO);
            sendMypos.putExtra(Intent.EXTRA_SUBJECT,"Min position");
            sendMypos.putExtra(Intent.EXTRA_TEXT, position);
            sendMypos.setData(Uri.parse("mailto:"));
            sendMypos.setType("message/rfc822");
            startActivity(Intent.createChooser(sendMypos,"Email klient"));
        }


    });

}

}

My Manifest:

 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
Spoofy
  • 621
  • 4
  • 9
  • 27

2 Answers2

0

I guess that the methid getLastKnownLocation returns null becuase the emulator start with no position?

Yes, or the provider is disabled, or the provider has not ever been used since the device was rebooted, etc. getLastKnownLocation() can easily return null.

if so how can i do this in another way?

You are welcome to use getLastKnownLocation(). You just have to check the result for null and not use it blindly.

You are also welcome to use requestSingleUpdate() to try to get a location fix by actually powering up the GPS briefly.

However, you cannot force the user and device to give you a location. If the user chooses to keep GPS disabled, that is the user's choice. If the device cannot get a GPS fix, you cannot force the user to move to a location that has GPS access. And so on.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • thank you for you fast answer! Do you meen tha i hafe to check for null like this: ` ** if(location == null){ Toast.makeText(getBaseContext(),"No locationservice found!",Toast.LENGTH_SHORT).show(); }else{ Double lat = location.getLatitude(); Double lng = location.getLongitude(); position = Double.toString(lat + lng); }**` – Spoofy Mar 18 '15 at 00:13
  • @Spoofy: Whether a `Toast` is the right answer is up to you, but you need to see if `location` is `null` before you start calling methods on it. – CommonsWare Mar 18 '15 at 11:32
  • is it not possible to generate long and lat in Android studio Like you can do in Eclipse with DDM? – Spoofy Mar 18 '15 at 20:00
  • @Spoofy: The limitations I describe here have nothing to do with IDE. You get the same results on Eclipse, Android Studio, IntelliJ IDEA, NetBeans, emacs, vi, Notepad, etc. – CommonsWare Mar 18 '15 at 21:03
0

Emulator is actually not an appropriate thing to test GPS (as emulator is running in a PC that has no GPS device.)

Test it in a real device instead. In most cases real device works fine. But if you face the same issue then it will help to debug in depth.

Mohammad Arman
  • 7,020
  • 2
  • 36
  • 50