AsyncTask不会终止

时间:2017-08-07 01:28:42

标签: android google-maps android-asynctask

我正在尝试使用异步任务渲染并向谷歌地图添加标记,但地图永远不会停止加载。我正确使用doInBackground和onPostExecute方法吗?我真的不确定如何解决这个问题。

以下是我的两个异步任务类

public class addMarkers extends AsyncTask<Void, String, GoogleMap> {

    String result;
    ProgressDialog pdia;

    protected void onPostExecute(Result result) {

        pdia.dismiss();
    }

    @Override
    protected GoogleMap doInBackground(Void... params) {
        publishProgress("Loading..."); // Calls onProgressUpdate()
        MapsFragment.HOME = new LatLng(30.284273, -97.744777);
        mGoogleMap.addMarker(new MarkerOptions().position(HOME));
        return mGoogleMap;
    }

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
        pdia = ProgressDialog.show(getContext(), "Please wait", "Loading data from database.", true, false);
    }

    @Override
    protected void onProgressUpdate(String... text) {

    }
}

public class renderMap extends AsyncTask<Void, String, View>{
    ProgressDialog pdia;
    LayoutInflater inflater;
    ViewGroup container;

    renderMap(LayoutInflater inflater, ViewGroup container){
        this.inflater = inflater;
        this.container = container;
    }

    protected void onPostExecute(String result) {

        pdia.dismiss();
    }

    @Override
    protected View doInBackground(Void... params) {
        publishProgress("Loading..."); // Calls onProgressUpdate()
        mView = inflater.inflate(R.layout.fragment_maps, container, false);
        return mView;
    }

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
        pdia = ProgressDialog.show(getContext(), "Please wait", "Loading data from database.", true, false);
    }

    @Override
    protected void onProgressUpdate(String... text) {

    }
}

这是我尝试加载地图时屏幕的样子。 enter image description here

1 个答案:

答案 0 :(得分:0)

我们不应该在其他线程中执行UI操作,只应在UI线程中执行UI操作,因此请删除Async任务并在UI线程中添加代码。