Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WindowManager$BadTokenException

Tags:

android

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....");
like image 469
Jignesh Ansodariya Avatar asked Aug 08 '11 04:08

Jignesh Ansodariya


People also ask

How does the window manager protect against badtokenexception?

The window manager protects against this by requiring applications to pass their application's window token as part of each request to add or remove a window. If the tokens don't match, the window manager rejects the request and throws a BadTokenException.

What happens if the window manager doesn't have window tokens?

If the tokens don't match, the window manager rejects the request and throws a BadTokenException. Without window tokens, this necessary identification step wouldn't be possible and the window manager wouldn't be able to protect itself from malicious applications.

What is badtokenexception in Android view?

android.view.WindowManager$BadTokenException: Unable to add window" This exception occurs when the app is trying to notify the user from the background thread (AsyncTask) by opening a Dialog.

What is a window token?

As its name implies, a window token is a special type of Binder token that the window manager uses to uniquely identify a window in the system. Window tokens are important for security because they make it impossible for malicious applications to draw on top of the windows of other applications.


2 Answers

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

} 
like image 178
Reno Avatar answered Oct 19 '22 02:10

Reno


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....");
like image 22
Andro Selva Avatar answered Oct 19 '22 02:10

Andro Selva