5

I am trying to put progress dialog on Click event of ListView as mentioned in code below but I am getting error "WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44eddc70 is not valid; is your activity running?" can you give me any solution for this ?

code

 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();
        }

    });

My file name is Add_Entry.java and error comes in line

ProgressDialog.show(Add_Entry.this,
                    "", "Please Wait....");
Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
Jignesh Ansodariya
  • 12,583
  • 24
  • 81
  • 113

3 Answers3

5

You are trying to update the UI from a thread. You can't do that.

Use the Handler mechanism to update UI components.

Code taken from the website : Here the Handler class is used to update a ProgressBar view in a background Thread.

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();
  }

} 
Reno
  • 33,594
  • 11
  • 89
  • 102
4
WindowManager$BadTokenException 

This occurs mostly because of bad context reference. To avoid this, try replacing your code,

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

with this,

 ProgressDialog.show(v.getRootView().getContext(),  "", "Please Wait....");
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • ok but by trying this I got another error like "Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() " on line parentActivity.startChildActivity("SorceOfStress", edit); – Jignesh Ansodariya Aug 08 '11 at 05:15
  • Which means you are not making use of handlers. Refer to my answer here.http://stackoverflow.com/questions/6894698/rotating-wheel-progress-dialog-while-deleting-folder-from-sd-card/6894744#6894744 – Andro Selva Aug 08 '11 at 06:18
0

Use like this

final ProgressDialog pd = new ProgressDialog(Add_Entry.this).show(Add_Entry.this,"","Please wait...", true);
Rasel
  • 15,499
  • 6
  • 40
  • 50