0

I am having a slight problem in Android Async Task. In my MainActivity, I am calling GetEventAsyncTask which will execute the method inside called retrieveEventJSON:

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    context = this;
    public void onSingleTap(float x, float y) {
            final Point point = mMapView.toMapPoint(x, y);
                eventModel.setEventX(String.valueOf(point.getX()));
                eventModel.setEventY(String.valueOf(point.getY()));
                 new MyAsyncTask(new MyAsyncTask.OnRoutineFinished() {
                        public void onFinish() {
                             CreateEvent.createEventDialog(context, point.getX(),
                                    point.getY(), eventAddress);  //this will be called after the task finishes
                        }
                    }).execute(eventModel);         
            }
    });
    new GetEventAsyncTask().execute();
}

In my GetEventAsyncTask, basically I am just retrieving the data returned from JSON and save them into an array:

public class GetEventAsyncTask extends AsyncTask<Event, Integer, Double> {
EventController eventCtrl = new EventController();
String eventAddress;
Event eventModel = new Event();

public interface OnRoutineFinished{  //interface
    void onFinish();
}
private OnRoutineFinished mCallbacks;
public GetEventAsyncTask(OnRoutineFinished callback){ //constructor with interface
    mCallbacks = callback;
}

public GetEventAsyncTask(){} //empty constructor to maintain compatibility

@Override
protected Double doInBackground(Event... params) {

    try {
        eventAddress = eventCtrl.getStreetAddressFromGeometry(eventModel.getEventX(), eventModel.getEventY());
        eventCtrl.retrieveEventJSON();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return null;
}

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

protected void onProgressUpdate(Integer... progress) {
}
}

Then when the navigation drawer item onselected, I am calling the plotting marker on map method which takes in the array I saved just now:

case 0:
        eventCtrl.plotEventOnMap(context);
        break;

I tried to print out the data retrieved in retrieveJSON and it did printed out. But somehow, when I tried to plot onto the map, it does not shows anything. I wonder which part that I overlapped or reinitialize some Object?

The strange thing is if I put getEventAsyncTask under MainActivity, it did run and retrieved the data. But however, if I shifted the getEventAsyncTask out as an individual class, it stopped working. I wonder why is it so?

Thanks in advance.

  • what do mean for stopped working? – duggu Nov 12 '14 at 08:06
  • As in by right it should plot something onto my map, but after I shifted out as an individual class, it just not plotting anymore. Nope, no error message at all. –  Nov 12 '14 at 08:07
  • @IWasSoLost have a look this http://stackoverflow.com/questions/16545022/call-asynctask-from-another-class – duggu Nov 12 '14 at 08:08
  • Who is responsible of updating `eventModel` in your code ? You're getting value from it but it's never modified. – Michael Laffargue Nov 12 '14 at 08:16
  • @MichaelLaffargue I've updated the question already. Does it clearer now? It's quite complicated –  Nov 12 '14 at 08:23
  • Looks like a mess, what is `MyAsyncTask`, why is the a function in the middle of `onCreate`. Where does the `eventModel` in `onSingleTap` comes from?. I think you should really clear things out and clarify the question – Michael Laffargue Nov 12 '14 at 09:12
  • You have `eventCtrl` that is local to class GetEventAsyncTask. I guess in your MainActivity you have another field of the same name. No idea why it worked in the first place, but that's two different instances. So filling one won't have effect on the other. – Fildor Nov 12 '14 at 09:22

0 Answers0