Android : BadTokenException when I want to show a progresssDialog a second time
I have a problem that I can't solve...
In my Activity, I instantiate a class like this :
MapView mapView = (MapView) findViewById(R.id.mapview);
myMap = new Map(mapView, this);
The constructor looks like this
public Map(MapView mapView, Context context) {
this开发者_StackOverflow.context = context;
this.mapView = mapView;
}
And what I want to do is to show a progressDialog during a process of this class, so, in Map, I got
private void showPath() {
progressDialog = ProgressDialog.show(context, "Veuillez patienter", "Calcul de l'itinéraire en cours...", true, false);
Thread thread = new Thread(this);
thread.start();
}
When thread is over, I do
progressDialog.dismiss();
This works ! But only one time... If I click on the back button, and re open my activity, I got a BadTokenException
05-06 23:27:15.941: ERROR/AndroidRuntime(1247): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@44ecc8e8 is not valid; is your activity running?
I've tryed all solutions I found, but no one works... Even use a class who extends AsyncTask.
Thank you for your help
As what the error message told you, it's because you try to show a dialog in a Activity but the Activity is not running(have already been finished?). So before you show a dialog, you may want to make sure that the Activity is not finished:
public class MyActivity extends Activity {
public void showDialog(Dialog dialog) {
if (!isFinishing()) {
// If activity is not finished, then show this dialog.
dialog.show();
}
}
}
精彩评论