5

首先,对不起我的英语不好,我会说葡萄牙语,我肯定会犯一些英语语法错误。

好吧,我正在尝试做一个 AsyncTask 类来从 googleMap 加载地图,但它显示了以下错误消息:

06-25 18:47:03.062: E/AndroidRuntime(8021): FATAL EXCEPTION: AsyncTask #1
06-25 18:47:03.062: E/AndroidRuntime(8021): java.lang.RuntimeException: An error occured while executing doInBackground()
06-25 18:47:03.062: E/AndroidRuntime(8021):     at java.lang.Thread.run(Thread.java:856)
06-25 18:47:03.062: E/AndroidRuntime(8021): Caused by: java.lang.IllegalStateException: Not on the main thread

我的主类叫 AsyncTask 类

public class ExemploGPSSimples extends FragmentActivity {

    protected GoogleMap map;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.exmeplo_gps);
    }

    @Override
    protected void onResume() {
        super.onResume();
        configuraGPS();
    }

    private void configuraGPS() {
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.fragment_gps);
        map = mapFragment.getMap();
        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);  
        //LatLng local=pegaPosicaoAtual();
        Localizador localizador = new Localizador(this);
        String endereco="Av. João Pessoa, 1091, Porto Alegre, RS";
        LatLng local = localizador.getEndereco(endereco);
        SincronizaMapa sincronizaMapa = new SincronizaMapa(this,local,map);
        sincronizaMapa.execute();
        adicionaMarcador(map,local);
        map.setMyLocationEnabled(true);     
    }

    private void adicionaMarcador(GoogleMap map2, LatLng local) {
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(local).title("Luiz").snippet("Casa");
        Marker marker = map2.addMarker(markerOptions);
    }


}

最后是 AsyncTask 类

public class SincronizaMapa extends AsyncTask<Void, Void, Void> {

    private final Activity activity;
    private final LatLng local;
    private final GoogleMap map;
    private ProgressDialog progressDialog;

    public SincronizaMapa(Activity activity, LatLng local,
            GoogleMap map) {
                this.activity = activity;
                this.local = local;
                this.map = map;

    }

    @Override
    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(activity, "Sincronizando mapa", "Aguarde...",true,true);
    }

    @Override
    protected Void doInBackground(Void... params) {
        CameraPosition position = new CameraPosition.Builder()
        .target(local)
        .bearing(0)
        .tilt(0)
        .zoom(17)
        .build();
        CameraUpdate update = CameraUpdateFactory.newCameraPosition(position);
        map.animateCamera(update);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        progressDialog.dismiss();
    }

}

谢谢大家的帮助。

4

2 回答 2

8

map.animate 修改 UI 组件(地图),因此必须在 UI 线程上完成。您可以将需要在 UI 线程上的代码移动到 onPostExecute

于 2013-06-25T23:33:22.683 回答
5

问题是您试图从 UI 线程以外的线程更新 UI。asyncTasks 允许您在保持 UI 线程空闲的同时执行后台工作 (doInBackgroud),然后相应地更新 UI (onPostExecute)。

您应该将 doInBackground 的主体放在 onPostExecute 的主体中——在 UI 线程上运行的方法。

于 2013-06-25T23:35:13.440 回答