2011-08-08 14 views
5

Estoy tratando de poner el cuadro de diálogo de progreso en el evento Click de ListView como se menciona en el siguiente código, pero obtengo el error "WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running?" ¿me puede dar alguna solución para esto?

código

final ListView lv1 = (ListView) findViewById(R.id.list); 
    lv1.setAdapter(new EfficientAdapter(this)); 

    lv1.setTextFilterEnabled(true); 

    lv1.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> a, View v, 
       final int position, long id) { 
      final ProgressDialog pd = ProgressDialog.show(Add_Entry.this, 
        "", "Please Wait...."); 
      new Thread() { 
       public void run() { 

        if (lv1.getItemAtPosition(position).equals(0)) { 

         Intent edit = new Intent(getApplicationContext(), 
           SourceOfStress.class); 
         TabGroupActivity parentActivity = (TabGroupActivity) getParent(); 
         edit.putExtra("currActi", "AddEntry"); 
         parentActivity.startChildActivity("SorceOfStress", 
           edit); 

        } 
        if (lv1.getItemAtPosition(position).equals(1)) { 
         Intent edit = new Intent(getParent(), 
           SourceOFSymptoms.class); 
         TabGroupActivity parentActivity = (TabGroupActivity) getParent(); 
         edit.putExtra("currActi", "AddEntry"); 
         parentActivity.startChildActivity(
           "SourceOFSymptoms", edit); 
        } 
        if (lv1.getItemAtPosition(position).equals(2)) { 
         Intent edit = new Intent(getParent(), 
           Stress_Resilliance.class); 
         TabGroupActivity parentActivity = (TabGroupActivity) getParent(); 
         edit.putExtra("currActi", "AddEntry"); 
         parentActivity.startChildActivity(
           "Stress_Resilliance", edit); 
        } 
        pd.dismiss(); 
       } 
      }.start(); 
     } 

    }); 

Mi nombre del archivo es Add_Entry.java y error viene en línea

ProgressDialog.show(Add_Entry.this, 
        "", "Please Wait...."); 

Respuesta

4

Usted está intentando actualizar la interfaz de usuario de un hilo. No puedes hacer eso.

Utilice Handler mechanism para actualizar los componentes de la IU.

Código tomado del sitio web: Aquí, la clase Handler se usa para actualizar una vista ProgressBar en un hilo de fondo.

package de.vogella.android.handler; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.View; 
import android.widget.ProgressBar; 
import android.widget.TextView; 

public class ProgressTestActivity extends Activity { 
    private Handler handler; 
    private ProgressBar progress; 
    private TextView text; 


/** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    progress = (ProgressBar) findViewById(R.id.progressBar1); 
    text = (TextView) findViewById(R.id.textView1); 

    } 

    public void startProgress(View view) { 
    // Do something long 
    Runnable runnable = new Runnable() { 
     @Override 
     public void run() { 
     for (int i = 0; i <= 10; i++) { 
      final int value = i; 
      try { 
      Thread.sleep(2000); 
      } catch (InterruptedException e) { 
      e.printStackTrace(); 
      } 
      progress.post(new Runnable() { 
      @Override 
      public void run() { 
       text.setText("Updating"); 
       progress.setProgress(value); 
      } 
      }); 
     } 
     } 
    }; 
    new Thread(runnable).start(); 
    } 

} 
+0

Creo que esta parte del artículo se ha ido . –

+0

@Brias Lo he corregido. Parece que Vogella reorganizó su sitio web. – Reno

0

uso como esto

final ProgressDialog pd = new ProgressDialog(Add_Entry.this).show(Add_Entry.this,"","Please wait...", true); 
+0

oh querida Rasel cuando pd se define como definitiva de cómo podemos volver a asignar un valor a la EP? (Según mi conocimiento) –

+0

Oh.really? Utilice como above.Thing usted tiene que cuidar es la creación de objetos utilizando ProgressDialog nuevo – Rasel

+1

@Jignesh Ansodariya está utilizando un contexto incorrecto, por lo tanto, intente usar el contexto correcto. –

3
WindowManager$BadTokenException 

Esto ocurre principalmente debido a malo referencia de contexto Para evitar esto, intente reemplazar el código,

ProgressDialog.show(Add_Entry.this, "", "Please Wait...."); 

con esto,

ProgressDialog.show(v.getRootView().getContext(), "", "Please Wait...."); 
+0

bien, pero al intentar esto recibí otro error como "Causado por: java.lang.RuntimeException: No se puede crear el controlador dentro de la cadena que no ha llamado Looper.prepare() " en la línea parentActivity.startChildActivity ("SorceOfStress", \t \t \t \t \t \t \t \t \t editar); –

+0

Lo que significa que no está utilizando manejadores. Consulte mi respuesta aquí.http: //stackoverflow.com/questions/6894698/rotating-wheel-progress-dialog-while-deleting-folder-from-sd-card/6894744#6894744 –

Cuestiones relacionadas