如何在android中的AsyncTask片段中传递listner

时间:2018-03-23 04:36:01

标签: android

这是我的AsyncTask班级

public class DistanceAsyncTask extends AsyncTask<String, String, String> {
    private TaskListener listener;
    String distance = "";
    String tag[] = {"text"};  //will give distance as string e.g 1.2 km
// or tag[] = {"value"} if you want to get distance in metre e.g. 1234

    HttpResponse response = null;

    public DistanceAsyncTask(TaskListener listener) {
        this.listener = listener;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... strings) {

        try {
            String url = "http://maps.google.com/maps/api/directions/xml?origin=" + strings[0] + "&destination=" + strings[1] + "&sensor=false&units=metric";
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(url);
            response = httpClient.execute(httpPost, localContext);
            InputStream is = response.getEntity().getContent();
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = builder.parse(is);
            if (doc != null) {
                NodeList nl;
                ArrayList args = new ArrayList();
                for (String s : tag) {
                    nl = doc.getElementsByTagName(s);
                    if (nl.getLength() > 0) {
                        Node node = nl.item(nl.getLength() - 1);
                        args.add(node.getTextContent());
                    } else {
                        args.add(" - ");
                    }
                }
                distance = String.format("%s", args.get(0));
            } else {
                System.out.print("Doc is null");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return distance;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        listener.onTaskFinished(s);
    }

}

我这样称呼它:

  new DistanceAsyncTask((TaskListener) getTargetFragment()).execute(result.getData().getCheckin_lat() + "," + result.getData().getCheckin_long(), result.getData().getCompanybranch_lat() + "," + result.getData().getCompanybranch_long());

当我跑步时,我在PostExecute方法上获得了距离但是listener.onTaskFinished(s);在这一行中我得到了异常

Attempt to invoke interface method void com.example.nehaanand.gfofficeproject.network.listeners.TaskListener.onTaskFinished(java.lang.String) on a null object reference at com.example.nehaanand.gfofficeproject.Utils.DistanceAsyncTask.onPostExecute(DistanceAsyncTask.java:82) at com.example.nehaanand.gfofficeproject.Utils.DistanceAsyncTask.onPostExecute(DistanceAsyncTask.java:27)

请告诉我我做错了什么以及如何解决。

1 个答案:

答案 0 :(得分:1)

您需要在TaskListener

中传递DistanceAsyncTask的对象

您已在 TaskListener 中实施ChekInFragment,因此您需要在{{1}中传递 ChekInFragment 的引用}}

检查以下代码

使用此

DistanceAsyncTask

而不是这个

new DistanceAsyncTask(ChekInFragment.this).execute(result.getData().getCheckin_lat() + "," + result.getData().getCheckin_long(), result.getData().getCompanybranch_lat() + "," + result.getData().getCompanybranch_long());