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>