ProgressDialog 的问题

发布于 2024-11-14 19:20:49 字数 972 浏览 3 评论 0原文

我不明白以下错误:

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 的行上...

知道吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

[旋木] 2024-11-21 19:20:49

当显示对话框时活动被破坏时,通常会发生这种情况,例如在显示对话框时旋转或完成活动。

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

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.

可是我不能没有你 2024-11-21 19:20:49

我不确定问题是什么,但我建议您使用 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();
    }
}

要执行它,请在 onCreate() 内调用 new MyBackgroundTask().execute()

它比使用处理程序、线程组合要干净得多......

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...

找回味觉 2024-11-21 19:20:49
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。在启动 ProgressDialog 之前使用 setContentView

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文