Jignesh Ansodariya
Jignesh Ansodariya

Reputation: 12705

WindowManager$BadTokenException

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....");

Upvotes: 5

Views: 10370

Answers (3)

Reno
Reno

Reputation: 33792

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

} 

Upvotes: 5

Andro Selva
Andro Selva

Reputation: 54330

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....");

Upvotes: 4

Rasel
Rasel

Reputation: 15477

Use like this

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

Upvotes: 0

Related Questions