Android 无法使用AsyncTask更新进度UI

Android 无法使用AsyncTask更新进度UI,android,user-interface,Android,User Interface,我的android应用程序中有一个MapActivity,我想画一条从源到目标的路线,然后沿着找到的路径绘制多段线。我正在显示一个progressDialog,同时沿着路径绘制多段线 我认为绘制多段线是一项繁重的任务,因为它导致progressDialog停止旋转。因此,我将drawPolyline功能放在AsyncTask中 但我现在面临的问题是,我没有更新屏幕,但突然MapActivity屏幕闪烁并消失,我现在看到了主活动 为什么会这样? 我想知道在AsyncTask的后台更新UI在概念上是

我的android应用程序中有一个
MapActivity
,我想画一条从源到目标的路线,然后沿着找到的路径绘制
多段线。我正在显示一个
progressDialog
,同时沿着路径绘制
多段线

我认为绘制多段线是一项繁重的任务,因为它导致
progressDialog
停止旋转。因此,我将
drawPolyline
功能放在
AsyncTask

但我现在面临的问题是,我没有更新屏幕,但突然
MapActivity
屏幕闪烁并消失,我现在看到了主活动

为什么会这样? 我想知道在AsyncTask的后台更新UI在概念上是否正确?如果是,我们如何着手

注意:我通过放置断点检查了AsyncTask功能。它按预期工作,但drawPolyline会导致错误。 此外,drawPolyline也没有问题,因为在没有AsyncTask的情况下调用它时,它可以正常工作

以下是我的任务代码:

public class DrawRoute {

    private WeatherMapActivity listener;
    private GoogleMap mMap = WeatherMapActivity.mMap;
    private List<Step> stepsInThePath = WeatherMapActivity.stepsInThePath;

    public DrawRoute(WeatherMapActivity listener) {
        this.listener = listener;
        Log.d("imHere", "in the DrawRouteConstructor");
    }

    public void execute(){
        Log.d("imHere", "in the execute of DrawRoute");
        new DrawPolyline().execute();
    }

    private class DrawPolyline extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            Log.d("imHere", "in the PreExecute");
        }

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

            Log.d("imHere", "in the Background");
            Geocoder geocoder = new Geocoder(listener, Locale.getDefault());

            mMap.addPolyline(new PolylineOptions()
                    .add(stepsInThePath.get(0).startLocation, stepsInThePath.get(0).endLocation)
                    .width(8)
                    .color(Color.RED));

            mMap.addMarker(new MarkerOptions()
                    .title(MainActivity.sourceAddress)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.start_red))
                    .position(stepsInThePath.get(0).startLocation));

            mMap.addMarker(new MarkerOptions()
                    .title(MainActivity.destinationAddress)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.end_green))
                    .position(stepsInThePath.get(stepsInThePath.size()-1).endLocation));


            for(int i=1; i < stepsInThePath.size(); i++){
                mMap.addPolyline(new PolylineOptions()
                        .add(stepsInThePath.get(i-1).endLocation, stepsInThePath.get(i).endLocation)
                        .width(8)
                        .color(Color.RED));

                List<Address> addresses = null;
                try {
                    addresses = geocoder.getFromLocation(stepsInThePath.get(i).startLocation.latitude, stepsInThePath.get(i).startLocation.longitude, 1);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                String cityName = addresses.get(0).getLocality();

                if(i%5 == 0 || i == stepsInThePath.size()-1){
                    mMap.addMarker(new MarkerOptions()
                            .title(cityName)
                            .position(stepsInThePath.get(i).startLocation));
                }
            }

            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(stepsInThePath.get(0).startLocation, 5));

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            Log.d("imHere", "in the PostExecute");
//            progressDialog.dismiss();
        }
    }
}
public类DrawRoute{
私有活动侦听器;
私有谷歌地图mMap=weathermappactivity.mMap;
私有列表stepsInThePath=WeatherMapActivity.stepsInThePath;
公共绘制路线(WeatherMapActivity listener){
this.listener=listener;
Log.d(“此处”、“在DrawRouteConstructor中”);
}
public void execute(){
Log.d(“此处”、“在执行提取路线时”);
新建DrawPolyline().execute();
}
私有类DrawPolyline扩展异步任务{
@凌驾
受保护的void onPreExecute(){
Log.d(“在这里”,“在执行前”);
}
@凌驾
受保护的空位背景(空位…空位){
Log.d(“在这里”,“在背景中”);
Geocoder Geocoder=新的地理编码器(侦听器,Locale.getDefault());
mMap.addPolyline(新的PolylineOptions()
.add(stepsInThePath.get(0).startLocation,stepsInThePath.get(0).endLocation)
.宽度(8)
.颜色(颜色.红色));
mMap.addMarker(新标记选项()
.title(MainActivity.sourceAddress)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.start\u红色))
.position(stepsInThePath.get(0.station));
mMap.addMarker(新标记选项()
.title(主活动.目的地址)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.end_绿色))
.position(stepsInThePath.get(stepsInThePath.size()-1.endLocation));
对于(int i=1;i
任何建议都将不胜感激


谢谢

切勿尝试直接从
doInBackground()
方法更新UI元素。
doInBackground()
的目的是不通过在单独的线程中运行来阻止UI,从而从主线程(UI线程)中卸载一些工作。 因此,从
doInBackground()
,调用
publishProgress()
将进度传递给
onProgressUpdate()
函数来完成UI工作

AsyncTask
doInBackground()
将在后台运行,并且除了主线程之外,没有其他线程可以更新UI

您能做的是在
doInBackground()
中完成所有计算并返回它。然后画入
onPostExecute()
。或者您可以使用主线程处理程序从
doInBackground()
进行绘制


您可以使用onPostExecute()方法对ui进行任何更改,因此不必为此创建方法。

添加您的
AsyncTask
代码。@ADM我添加的代码正是我想问的。谢谢你的澄清。
Handler mainHandler = new Handler(Looper.getMainLooper());
Handler mainHandler = new Handler(context.getMainLooper());