我的活动继续显示对话框,似乎不做doInBackground

时间:2014-11-02 12:19:54

标签: android android-asynctask

我的活动一直显示对话框,好像不做doInBackground。它应该保持“正在加载”屏幕。  这是我的代码:

private class MapTask extends AsyncTask<Void, Void, Void> {
    protected ProgressDialog dialog;
    protected Context context;

    public MapTask(Context context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        this.dialog = new ProgressDialog(context, 1);
        this.dialog.setMessage("Loading");
        this.dialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {

        try {

            try {
                String countryName=country.getTitle();
                List<Address> address = new Geocoder(context).getFromLocationName(countryName, 1);
                if (address == null) {
                    Log.e(null,  "Not found");
                } else {
                    Address loc = address.get(0);
                    Log.e(null, loc.getLatitude() + " " + loc.getLongitude());
                    LatLng pos = new LatLng(loc.getLatitude(), loc.getLongitude());
                    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(pos, 6));
                    return null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        catch (Exception e) {
            Log.v("ASYNC", "ERROR : " + e);
            e.printStackTrace();
        }
        return null;
    }

}

有人能帮帮我吗?

2 个答案:

答案 0 :(得分:0)

您没有在任何地方致电dialog.dismiss()。您应该在onPostExecute的{​​{1}}中执行此操作:

AsyncTask

答案 1 :(得分:0)

实际上它正在做doInBackground的东西,问题是你没有在onPostExecute()方法中解雇对话框

只需在onPostExecute方法中添加dialog.dismiss()