ProgressDialog问题

时间:2011-06-07 11:49:11

标签: android progressdialog

我不明白以下错误:

Activity [myActivity] has leaked window
com.android.internal.policy.impl.PhoneWindow$DecorView@4050c3f8 that was
originally added here

这是我的代码:

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

显然,问题在于我实现了我的ProgressDialog ...

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

通常在显示对话框时销毁活动时发生这种情况,例如在显示对话框时旋转或完成活动。

尝试在活动暂停时添加关闭对话框。

答案 1 :(得分:1)

我不确定问题是什么,但我建议您使用AsyncTask

像这样的东西

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

要执行此操作,请在new MyBackgroundTask().execute()

内拨打onCreate()

它比使用处理程序,线程组合更清晰......

答案 2 :(得分:0)

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

在将主视图添加到活动之前,您尝试显示ProgressDialog。在开始setContentView之前使用ProgressDialog