Sunday 15 June 2014

java - Android How to getLat() and get Long() -


itemprop = "text">

im if a user is sending me an email long and lat and im getting to NullPointerException:

< P> attempt to invoke the virtual method 'double android.location.Location.getLatitude ()' on a null object reference

I think that with the method getLastKnownLocation () no status Start emulator because null returns? ?

If so, how can I do this in any other way

Here is my code:

  Public class MainActivity Extension Activity {Button BtnSend; TextView txtview; String email = "superman@hotmail.com"; String position; @ Override Protected Zero (Bundle Saved Instantstate) {Super. NET (Saved Instantstate); SetContentView (R.layout.activity_main); Find Txtview = (TextView) ViewById (R.id.textView); BtnSend = (button) findViewById (R.id.button); LocationManager lm = (LocationManager) getSystemService (context.LOCATION_SERVICE); Location Location = lm.getLastKnownLocation (LocationManager.GPS_PROVIDER); Double latitude = location. Gelititis (); Double LNG = location.getLongitude (); Position = double.string (latitude + lng); BtnSend.setOnClickListener (New View.OnClickListener) {@Override Public Zero onClick (see v) {String [] for = {"batman@hotmail.com"}; Intent to sendMypos = new intent (Intent.ACTION_SEND); sendMypos PutExtra (for Intent.EXTRA_EMAIL,); sendMypos.putExtra (Intent.EXTRA_SUBJECT, "Minimum Status"); sendMypos.putExtra (Intent.EXTRA_TEXT, status); sendMypos.setData (Uri.parse ("mailto:")); SendMypos.setType ("Message / RFC822"); startActivity (Intent.createChooser (sendMypos, "email klient"));}}); }  

}

Clear to me:

  Use of Android-permission: name = "android.permission.INTERNET" & gt; ; & Lt; Uses / Permissions & gt; & Lt; Usage-permission Android: name = "android.permission.ACCESS_FINE_LOCATION" & gt; & Lt; / Usage-permission & gt;  

I think methid getLastKnownLocation is blank because the emulator does not start the position ?

Yes, or the provider is disabled, or the device has never been used since the device was rebooted, etc. getLastKnownLocation () can easily come back to null .

If so, how can I do it any other way?

You get getLastKnownLocation () . All you have to do is check the result for null and do not use it indiscriminately.

In fact, you are welcome to try to fix a space by powering the GPS briefly. / P>

However, you can not force users and devices to give you space. If the user chooses to disable the GPS, then it is the user's choice. If the device can not get GPS fixed, then you can not force the user to transfer that has GPS access. And so on


No comments:

Post a Comment