Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've got the same problem. If I stay on the current activity, no problem, but if I leave the activity while <code>doInBackgroud</code> is running, <code>onPostExecute</code> will exit on the Toast line.</p> <p>To resolve the problem, you have to use a handler :<br /> In the class</p> <pre><code>private static final int TOAST = 0; private Handler mHandler = null; </code></pre> <p>In <code>OnCreate()</code></p> <pre><code>// Creation of the handler to display Toasts if (mHandler == null) { mHandler = new Handler() { @Override public void handleMessage(Message _msg) { switch (_msg.what) { case TOAST: Toast.makeText(ServerTabHost.this, (String)_msg.obj, Toast.LENGTH_LONG).show(); break; default : break; } super.handleMessage(_msg); } }; } </code></pre> <p>In <code>onPostExecute()</code> </p> <pre><code>Message msg = new Message(); msg.what = TOAST; msg.obj = "my toast message"; mHandler.sendMessage(msg); </code></pre> <p>In your code it looks like this :</p> <pre><code>public class statuspage extends MapActivity { // These two lines are for the handler private static final int TOAST = 0; private Handler mHandler = null; LocationManager locationManager; MapView mapView; Criteria criteria; Location location; Geocoder gc; Address address; String bestProvider; String LOCATION_SERVICE = "location"; String addressString = "Searching for Nearest Address"; StringBuilder sb; private MapController mapController; private MyLocationOverlay myLocation; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.statuspage); // Get Mapping Controllers etc // mapView = (MapView) findViewById(R.id.mapView); mapController = mapView.getController(); mapController.setZoom(17); mapView.setBuiltInZoomControls(true); // Add the MyLocationOverlay // myLocation = new MyLocationOverlay(this, mapView); mapView.getOverlays().add(myLocation); myLocation.enableCompass(); myLocation.enableMyLocation(); // Animates the map to GPS Position // myLocation.runOnFirstFix(new Runnable() { @Override public void run() { mapController.animateTo(myLocation.getMyLocation()); } }); // Creation of the handler to display Toasts if (mHandler == null) { mHandler = new Handler() { @Override public void handleMessage(Message _msg) { switch (_msg.what) { case TOAST: Toast.makeText(ServerTabHost.this, (String)_msg.obj, Toast.LENGTH_LONG).show(); break; default : break; } super.handleMessage(_msg); } }; } } @Override protected boolean isRouteDisplayed() { // Location Manager Intiation locationManager = (LocationManager) statuspage.this .getSystemService(LOCATION_SERVICE); criteria = new Criteria(); // More accurate, GPS fix. criteria.setAccuracy(Criteria.ACCURACY_FINE); // More accurate, GPS fix. bestProvider = locationManager.getBestProvider(criteria, true); location = locationManager.getLastKnownLocation(bestProvider); updateWithNewLocation(location); locationManager.requestLocationUpdates(bestProvider, 60000, 10, locationListener); // 1800000 = 30 Min return false; } class GeoCoder extends AsyncTask&lt;Void, Void, Void&gt; { String lat = "Acquiring"; String lng = "Acquiring"; @Override protected Void doInBackground(Void... params) { if (location != null) { /** * double latitude = myLocation.getMyLocation().getLatitudeE6(); * double longitude = * myLocation.getMyLocation().getLongitudeE6(); */ double latitude = location.getLatitude(); double longitude = location.getLongitude(); lat = "" + latitude; lng = "" + longitude; // gc = new Geocoder(statuspage.this, Locale.getDefault()); Geocoder gc = new Geocoder(getApplicationContext(), Locale.getDefault()); try { List&lt;Address&gt; addresses = gc.getFromLocation(latitude, longitude, 1); sb = new StringBuilder(); if (addresses != null &amp;&amp; addresses.size() &gt; 0) { address = addresses.get(0); int noOfMaxAddressLine = address .getMaxAddressLineIndex(); if (noOfMaxAddressLine &gt; 0) { for (int i = 0; i &lt; address .getMaxAddressLineIndex(); i++) { sb.append(address.getAddressLine(i)).append( "\n"); } addressString = sb.toString(); } } } catch (Exception e) { addressString = "Sorry, we are trying to find information about this location"; } } return null; } @Override protected void onPostExecute(Void result) { // Sending the Toast message through the handler Message msg = new Message(); msg.what = TOAST; msg.obj = "My toast message"; mHandler.sendMessage(msg); TextView scrollview = (TextView) findViewById(R.id.scrollview); // Latitude and Longitude TextView TextView etlongitude = (TextView) findViewById(R.id.etlongitude); TextView etlatitude = (TextView) findViewById(R.id.etlatitude); // TextView to display GeoCoder Address scrollview.setGravity(Gravity.CENTER); scrollview.setText("Your location:" + "\n" + "(Accurate to 500 meters)" + "\n" + (addressString)); Log.d("Address", (addressString)); // Latitude and Longitude TextView Display Coordinates // etlongitude.setText(lng); etlatitude.setText(lat); // Log.d("GeoCoder", "In-Task"); return; } </code></pre> <p>Personally, I was in a Fragment so I had to create the handler in the host activity and then pass it in the fragment constructor.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload