Skip to content Skip to sidebar Skip to footer

Android Asynctask #2 Fatal Exception

I am having some problem with AsyncTask #2 fatal exception in Android. So what I am trying to do is single tap on map, get the coordinates X and Y and pass them to AsyncTask() clas

Solution 1:

Call eventCtrl.plotEventOnMap(context); on postExecute instead. This method appears to change the UI which can't be done on doInBackground

Edit

You will have to call CreateEvent.createEventDialogonly after your AsyncTask finishes. For that I recomend creating an interface and pass it to your task. That will require some coding. Let's get started:

publicstaticclassMyAsyncTaskextendsAsyncTask<Event, Integer, Double> {
    publicinterfaceOnRoutineFinished{  //interfacevoidonFinish();
    }
    private OnRoutineFinished mCallbacks;
    publicMyAsyncTask(OnRoutineFinished callback){ //constructor with interface
        mCallbacks = callback;
    }

    publicMyAsyncTask(){} //empty constructor to maintain compatibility

    @Override
    protected Double doInBackground(Event... params) {
        try {
            eventAddress = eventCtrl.getStreetAddressFromGeometry(eventModel.getEventX(), eventModel.getEventY());
            eventCtrl.retrieveEventJSON();
            if (params.length == 1) {
                eventCtrl.createEvent(params[0]);
                // Refresh map after successfully added event
                eventCtrl.retrieveEventJSON();
                eventCtrl.plotEventOnMap(context);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        returnnull;
    }

    protectedvoidonPostExecute(Double result) {
        if(mCallbacks !=null)
            mCallbacks.onFinish(); //call interface on finish
    }

    protectedvoidonProgressUpdate(Integer... progress) {
    }
}

Next when you create your asyntask pass the event you want to call when the async finishes:

    new MyAsyncTask(new MyAsyncTask.OnRoutineFinished() {
        @Override
        public void onFinish() {
             CreateEvent.createEventDialog(context, point.getX(),
                    point.getY(), eventAddress);  //this will be called after the task finishes
        }
    }).execute(eventModel);

Post a Comment for "Android Asynctask #2 Fatal Exception"