Handler{40664220} sending message to a Handler on a dead thread

616 Views Asked by At

I am getting this error my code is, can anyone help please

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        new FetchAddressTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, Constants.USER_CURRENT_LOCATION);
    else
        new FetchAddressTask().execute(Constants.USER_CURRENT_LOCATION);


    class FetchAddressTask extends AsyncTask<LatLng, String, String> {
    @Override
    protected void onPreExecute() {
        // showDialog();
        // showMyDialog();
        DialogHelper.showTrasparentDialog(EnterBikeInfoActivity.this);
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(LatLng... latLngs) {

        String result = getCurrentLocationViaJSON(latLngs[0].latitude, latLngs[0].longitude);
        if (result == null)
            result = "";
        return result;
    }

    @Override
    protected void onPostExecute(String result) {
        if (result == null || result.length() == 0) {
            DialogHelper.showDialogMessage(BikeApp.getInstance(), "Unable to retrieve the address.");
        } else {
            addressEditText.setText(result);
        }

        DialogHelper.dismissTransparentDialog();
        super.onPostExecute(result);
    }
}


 public String getCurrentLocationViaJSON(double lat, double lng) {
    JSONObject jsonObj = getLocationInfo(lat, lng);
    Log.i("JSON string =>", jsonObj.toString());
    String currentLocation = "testing";
    // String street_address = null;
    // String postal_code = null;
    try {
        String status = jsonObj.getString("status").toString();
        Log.i("status", status);
        if (status.equalsIgnoreCase("OK")) {
            JSONArray results = jsonObj.getJSONArray("results");
            int i = 0;
            Log.i("i", i + "," + results.length()); // TODO delete this
            JSONObject r0 = results.getJSONObject(i);
            String v = r0.getString("formatted_address");
            currentLocation = v;
            Log.i("JSON Geo Locatoin =>", currentLocation);
            return currentLocation;
        }
    } catch (JSONException e) {
        Log.e("testing", "Failed to load JSON");
        e.printStackTrace();
    }
    return "";
}

getLocationInfo(lat, lng); funtion fetches address info from an http request, my flow is coming into FetchAddressTask doInBackground with correct address but when it return the address it give me this error message.

11-25 11:20:01.746: W/MessageQueue(2593): Handler{40664220} sending message to a Handler on a dead thread
11-25 11:20:01.746: W/MessageQueue(2593): java.lang.RuntimeException: Handler{40664220} sending message to a Handler on a dead thread
11-25 11:20:01.746: W/MessageQueue(2593):   at android.os.MessageQueue.enqueueMessage(MessageQueue.java:196)
11-25 11:20:01.746: W/MessageQueue(2593):   at android.os.Handler.sendMessageAtTime(Handler.java:457)
11-25 11:20:01.746: W/MessageQueue(2593):   at android.os.Handler.sendMessageDelayed(Handler.java:430)
11-25 11:20:01.746: W/MessageQueue(2593):   at android.os.Handler.sendMessage(Handler.java:367)
11-25 11:20:01.746: W/MessageQueue(2593):   at android.os.Message.sendToTarget(Message.java:349)
11-25 11:20:01.746: W/MessageQueue(2593):   at android.os.AsyncTask$3.done(AsyncTask.java:214)
11-25 11:20:01.746: W/MessageQueue(2593):   at java.util.concurrent.FutureTask$Sync.innerSet(FutureTask.java:253)
11-25 11:20:01.746: W/MessageQueue(2593):   at java.util.concurrent.FutureTask.set(FutureTask.java:113)
11-25 11:20:01.746: W/MessageQueue(2593):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:311)
11-25 11:20:01.746: W/MessageQueue(2593):   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
11-25 11:20:01.746: W/MessageQueue(2593):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
11-25 11:20:01.746: W/MessageQueue(2593):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
11-25 11:20:01.746: W/MessageQueue(2593):   at java.lang.Thread.run(Thread.java:1019)
0

There are 0 best solutions below