2011-05-13 33 views
6

Necesito mostrar el Alertdialog dentro de Asynctask en Android, pero no se mostrará en la excepción de tiros.diálogo de alerta dentro asynctask en android

05-13 14:59:35.522: WARN/System.err(1179): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 

Gracias.

Respuesta

12

No es posible manipular la IU desde el hilo de fondo. Use el controlador o los métodos onPre/Post de AsyncTask para eso.

2

Creo que this podría ser su problema: no se puede mostrar un cuadro de diálogo desde doInBackground.

3

Muestra el cuadro de diálogo en el método onPostExecute() en lugar de doInBackground.

Espero que ayude

0

Puede llamar publishProgress() derecha desde doInBackground() y manipular la interfaz de usuario en onProgressUpdate().

2

No hay problema para llegar al subproceso UI del subproceso de fondo (más claro: del método doInBackground()): solo tiene que ejecutar el método runOnUiThread() en su contexto. En mi caso lo estoy llamando desde doInBackground() cuerpo.

ContactsViewerActivity.this.runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        AlertDialog.Builder builder = new AlertDialog.Builder(ContactsViewerActivity.this); 
         builder.setTitle("Connection problem..") 
         .setMessage("Could not connect to " + url + ", " + 
           "try changing your host in the settings to default one :" + 
           getResources().getString(R.string.default_server_ip) + ":" + 
           getResources().getString(R.string.default_server_port)) 
         .setNegativeButton("OK", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           dialog.cancel(); 
          } 
         }); 
         AlertDialog alert = builder.create(); 
         alert.show(); 
       } 
      }); 
Cuestiones relacionadas