Problem with a ProgressDialog
I don't understand the following error:
Activity [myActivity] has leaked window
com.android.internal.policy.impl.PhoneWindow$DecorView@4050c3f8 that was
originally added here
Here is my code:
private ProgressDialog progression;
private Handler handler;
private Thread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Conteneur général
ui = new RelativeLayout(this);
progression = ProgressDialog.show(this, "SwissParl", "Init", true);
handler = new Handler() {
public void handleMessage(Message msg) {
progression.dismiss();
}
};
thread = new Thread() {
public void run() {
firstMethod();
SecondOne();
andTheLast();
开发者_如何学运维 handler.sendEmptyMessage(0);
}
};
thread.start();
setContentView(ui);
}
Apparently, the problem is on the line where I instanciate my ProgressDialog...
Any idea?
That happens normally when the activity is destroyed when showing a dialog, for example rotation or finishing activities while showing dialogs.
Try adding a dismiss dialog onPause of activity.
I'm not sure what the problem is, but i suggest you use an AsyncTask
Something like this
private class MyBackgroundTask extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(ParentActivity.this);
@Override
protected Void doInBackground(Void... param) {
firstMethod();
SecondOne();
andTheLast();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if(dialog.isShowing())
dialog.dismiss();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//dialog.setCancelable(false); //depending...
dialog.setMessage("Please Wait...");
dialog.show();
}
}
To execute it, call new MyBackgroundTask().execute()
inside onCreate()
It's far cleaner than using a handler,thread combination...
private ProgressDialog progression;
private Handler handler;
private Thread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Conteneur général
ui = new RelativeLayout(this);
setContentView(ui);
progression = ProgressDialog.show(this, "SwissParl", "Init", true);
handler = new Handler() {
public void handleMessage(Message msg) {
progression.dismiss();
}
};
thread = new Thread() {
public void run() {
firstMethod();
SecondOne();
andTheLast();
handler.sendEmptyMessage(0);
}
};
thread.start();
}
You are trying to show a ProgressDialog
before adding the main view to the activity.Use the setContentView
before you start the ProgressDialog
精彩评论