Question

i am following this tutorial to implement google direction API. In the tutorial the onclick and all codes related to inputing data are in a fragmentActivity. But i would like to use a fragment. i would like to know how to change the asyncTask class in the tutorial so that it is using the right Context to find my fragment. Here is my attempt.

public class GetDirectionsAsyncTask extends AsyncTask<Map<String, Object>, Object, ArrayList<LatLng>>
{
    public static final String USER_CURRENT_LATLNG = "user_current_latlng";
    public static final String DESTINATION_LATLNG = "destination_latlng";
    public static final String DIRECTIONS_MODE = "directions_mode";
    private MainActivity activity;
    private Closest fragment;
    private Exception exception;
    private ProgressDialog progressDialog;

    public GetDirectionsAsyncTask(Closest fragment, MainActivity activity)
    {
        super();
        this.activity = activity;
    }

    public void onPreExecute()
    {
        progressDialog = new ProgressDialog(this.fragment.getActivity());
        progressDialog.setMessage("Calculating directions");
        progressDialog.show();
    }

    @Override
    public void onPostExecute(ArrayList<LatLng> result)
    {
        progressDialog.dismiss();
        if (exception == null)
        {
            fragment.handleGetDirectionsResult(result);
        }
        else
        {
            processException();
        }
    }
 @Override
    protected ArrayList<LatLng> doInBackground(Map<String, Object>... params)
    {
        Map<String, Object> paramMap = params[0];
        try
        {
            LatLng fromPosition = (LatLng) paramMap.get(USER_CURRENT_LATLNG);
            LatLng toPosition = (LatLng) paramMap.get(DESTINATION_LATLNG);
            GMapV2Direction md = new GMapV2Direction();
            Document doc = md.getDocument(fromPosition, toPosition, (String)paramMap.get(DIRECTIONS_MODE));
            ArrayList<LatLng> directionPoints = md.getDirection(doc);
            return directionPoints;
        }
        catch (Exception e)
        {
            exception = e;
            return null;
        }
    }

    private void processException()
    {
        Toast.makeText(activity, "Error retriving data", Toast.LENGTH_LONG).show();
    }
}

The minute it needs to know the Context, it crashes in logcat with a nullpointer exception. if u realise, the first time it needs the context is on progressDialog = new ProgressDialog(this.fragment.getActivity());

Was it helpful?

Solution

Simply replace this.fragment.getActivity() with this.activity since you already have reference to the activity.

Technically, to make it a more reusable and decoupled class I would replace the reference to MainActivity and simply call it Context since that's really all you need it for and don't really care if it's the MainActivity or some other Activity that this later lives on.

OTHER TIPS

Instead

 progressDialog = new ProgressDialog(this.fragment.getActivity());

use

 progressDialog = new ProgressDialog(activity);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top