使用Google地图和addMarker与TaskSync?

时间:2014-01-23 11:14:59

标签: android google-maps android-asynctask

如何使用Google地图并使用TaskSync添加标记?我有一个错误:

01-23 12:11:26.226: E/AndroidRuntime(1878): Caused by: java.lang.NullPointerException
01-23 12:11:26.226: E/AndroidRuntime(1878):     at com.example.maps3.MainActivity$MiTareaAsincronaDialog.onPreExecute(MainActivity.java:155)

由于

main.java

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    latitud = 40.42;
    longitud = -3.688889;

    mapa = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapa)).getMap();
    CameraPosition cameraPosition = new CameraPosition.Builder().target(
            new LatLng(latitud, longitud)).zoom(18).build();
    mapa.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    mapa.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    pintarRuta();

    btnAsyncDialog = (Button)findViewById(R.id.btnAsyncDialog);

    btnAsyncDialog.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pDialog.setMessage("Procesando...");
            pDialog.setCancelable(true);
            pDialog.setMax(100);

            tarea2 = new MiTareaAsincronaDialog();
            tarea2.execute();
        }
    });
}

public void pintarRuta() {

    ArrayList<LatLng> markerPoints = new ArrayList<LatLng>();

    LatLng point = new LatLng(latitud, longitud);           

    markerPoints.add(point);

    tarea2 = new MiTareaAsincronaDialog();
    tarea2.execute();       
}

private void tareaLarga(){
    try { 
        Thread.sleep(1000); 
    } catch(InterruptedException e) {}
}

private class MiTareaAsincronaDialog extends AsyncTask<Void, Integer, Boolean> {

    protected Boolean doInBackground(Void... params) {

        for(int i=1; i<=10; i++) {
            tareaLarga();
            publishProgress(i*10);

            if(isCancelled()) {
                break;
            }
        }
        return true;
    }

    protected void onProgressUpdate(Integer... values) {
        int progreso = values[0].intValue();

        pDialog.setProgress(progreso);

        mapa.addMarker(new MarkerOptions()
        .position(point)
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
        .title("Posición inicial 1"));

    }

    protected void onPreExecute() {

        pDialog.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                MiTareaAsincronaDialog.this.cancel(true);
            }
        });

        pDialog.setProgress(0);
        pDialog.show();
    }

    protected void onPostExecute(Boolean result) {
        if(result){
            pDialog.dismiss();
            Toast.makeText(MainActivity.this, "Tarea finalizada!", Toast.LENGTH_SHORT).show();
        }
    }
 }

2 个答案:

答案 0 :(得分:0)

onPreExecute内写下progressdialog初始化代码,而不是onClick,如下所示:

 protected void onPreExecute() {

       pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pDialog.setMessage("Procesando...");
        pDialog.setCancelable(true);
        pDialog.setMax(100);
       pDialog.setOnCancelListener(new OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            MiTareaAsincronaDialog.this.cancel(true);
        }
    });

    pDialog.setProgress(0);
    pDialog.show();
}

答案 1 :(得分:0)

问题出在您的pintarRuta()功能中。您正在从此函数执行MiTareaAsincronaDialog() asynctask,但此时progressDialog实例不可用,因为您在单击btnAsyncDialog按钮时创建了progressDialog实例。解决方案是在onCreate()中初始化progressDialog:

    pDialog = new ProgressDialog(MainActivity.this);
    pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    pDialog.setMessage("Procesando...");
    pDialog.setCancelable(true);
    pDialog.setMax(100);
    pintarRuta();

    btnAsyncDialog = (Button)findViewById(R.id.btnAsyncDialog);

    btnAsyncDialog.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            tarea2 = new MiTareaAsincronaDialog();
            tarea2.execute();
        }
    });

或者在asynctask&#39; onPreExecute()