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....");
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.
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.
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.
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.
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();
}
}
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....");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With