2010-09-01 6 views
76

Non capisco perché sto ricevendo questo errore. Sto usando AsyncTask per eseguire alcuni processi in background.Impossibile creare il gestore all'interno del thread che non ha chiamato Looper.prepare() all'interno di AsyncTask per ProgressDialog

ho:

protected void onPreExecute() 
{ 
    connectionProgressDialog = new ProgressDialog(SetPreference.this); 
    connectionProgressDialog.setCancelable(true); 
    connectionProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
    connectionProgressDialog.setMessage("Connecting to site..."); 
    connectionProgressDialog.show(); 

    downloadSpinnerProgressDialog = new ProgressDialog(SetPreference.this); 
    downloadSpinnerProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
    downloadSpinnerProgressDialog.setMessage("Downloading wallpaper..."); 
} 

Quando mi trovo in doInBackground() a seconda di una condizione che:

[...]  
connectionProgressDialog.dismiss(); 
downloadSpinnerProgressDialog.show(); 
[...] 

Ogni volta che provo downloadSpinnerProgressDialog.show() ricevo l'errore.

Qualche idea ragazzi?

risposta

103

Procedimento show() deve essere richiamati dalla User-Interface (UI) thread, mentre doInBackground() gira su filo differente che è la ragione principale per cui AsyncTask creata.

È necessario chiamare show() in onProgressUpdate() o in onPostExecute().

Ad esempio:

class ExampleTask extends AsyncTask<String, String, String> { 

    // Your onPreExecute method. 

    @Override 
    protected String doInBackground(String... params) { 
     // Your code. 
     if (condition_is_true) { 
      this.publishProgress("Show the dialog"); 
     } 
     return "Result"; 
    } 

    @Override 
    protected void onProgressUpdate(String... values) { 
     super.onProgressUpdate(values); 
     connectionProgressDialog.dismiss(); 
     downloadSpinnerProgressDialog.show(); 
    } 
} 
+3

che non ha fatto il trucco per me, solo provato "AlertDialog . Builder builder = new AlertDialog.Builder (context); " e ha aggiunto la configurazione del builder. Cercando di mostrarlo all'interno di onProgressUpdate si ottiene esattamente lo stesso errore. Qualche idea ? – ins0m

+0

Grazie Konstantin, non mi ero reso conto che potresti sovrascrivere suProgressUpdate con qualsiasi tipo di variabile usando publishProgress(). Mi ha davvero aiutato, per aggiornare semplicemente con i messaggi di brindisi. – wired00

75

Ho avuto un problema simile, ma dalla lettura di questa domanda ho pensato che avrei potuto girare su thread UI:

YourActivity.this.runOnUiThread(new Runnable() { 
    public void run() { 
     alertDialog.show(); 
    } 
}); 

sembra fare il trucco per me.

+0

thanx ha funzionato ... – Debasish

0
final Handler handler = new Handler() { 
     @Override 
     public void handleMessage(final Message msgs) { 
     //write your code hear which give error 
     } 
     } 

new Thread(new Runnable() { 
    @Override 
    public void run() { 
    handler.sendEmptyMessage(1); 
     //this will call handleMessage function and hendal all error 
    } 
    }).start(); 
1

ho avuto un momento difficile fare questo lavoro anche la soluzione per me è stato quello di utilizzare sia hyui e risposte Konstantin,

class ExampleTask extends AsyncTask<String, String, String> { 

// Your onPreExecute method. 

@Override 
protected String doInBackground(String... params) { 
    // Your code. 
    if (condition_is_true) { 
     this.publishProgress("Show the dialog"); 
    } 
    return "Result"; 
} 

@Override 
protected void onProgressUpdate(String... values) { 

    super.onProgressUpdate(values); 
    YourActivity.this.runOnUiThread(new Runnable() { 
     public void run() { 
      alertDialog.show(); 
     } 
    }); 
} 

} 
+0

Ho pensato che le cose fatte in 'onProgressUpdate()' sarebbero state automaticamente eseguite sul thread dell'interfaccia utente –

Problemi correlati