其他线程不会让我更改“活动"布局!安卓 [英] Other thread wont let me change Activity layout!? Android

查看:68
本文介绍了其他线程不会让我更改“活动"布局!安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案:

它可能不是最佳或最漂亮的解决方案,但它可以工作.启动一个新线程,以便在新线程检索,分配和中断主线程时,主线程可以显示一个ProgressDialog,并使用新的可运行线程来启动视图.为了在新线程中的所有活动正在进行时显示ProgressDialog,必须如上所述在主线程中启动ProgressDialog,然后在新线程结束时将其关闭.必须在新线程启动之前立即启动ProgressDialog,并且您要等待的所有内容都必须进入新线程.记住,如果要在等待并显示ProgressDialog时进行UI更改,则必须使用新的runnable中断主线程,这将通过使用runOnUiThread在其run()方法中进行更改.

It might not be the best or the prettiest solution, but it works. Start a new thread so the main thread can display a ProgressDialog while the new thread retrieves, assigns and interrupts the main thread with a new runnable to initiate views. For the ProgressDialog to be shown while all the activity in the new thread is going on, the ProgressDialog must, as mentioned, be started in the main thread and dismissed of course at the end of the new thread. The ProgressDialog must be started as the last thing right before the new thread starts, and everything you wanna wait for must go into the new thread. And remember, if you wanna make UI alterations while waiting and showing ProgressDialog you must interrupt the main thread with a new runnable, which makes the alterations in it's run() method, by using runOnUiThread.

public void onCreate(Bundle savedInstanceState) {
                ...

                ((ScrollView) findViewById(R.id.contentContainer)).addView(getLayoutInflater().inflate(R.layout.myCustomContentLayout, null));

                ((TextView) findViewById(R.id.name)).setText(customer.getProperty("name").getValue().toString());

                final ProgressDialog progressDialog = ProgressDialog.show(this, "Loading", "Please wait...");

                new Thread(new Runnable() {
                    protected void run() {
                        ... //get some data, fill some lists and assign some variables...

                        runOnUiThread(new Runnable() {
                                          public void run() {
                                              initiateActivity();
                                          }
                                      });

                        progressDialog.dismiss();

                        return true;
                    }
                }).start();
}

原始帖子:

您好,非常感谢您的阅读. :)

Hello all and many thanks for reading. :)

真的不可能从与主UI线程不同的线程更改活动布局吗?

Is it really not possible to change your activity layout from a different thread than the main UI-thread?

我正在尝试在ProgressDialog运行时从另一个线程将数据填充到表中.这样.

I'm trying to fill data into a table from another thread while a ProgressDialog is running. Like this.

public void onCreate(Bundle savedInstanceState) {
                ...

                ((ScrollView) findViewById(R.id.contentContainer)).addView(getLayoutInflater().inflate(R.layout.myCustomContentLayout, null));

                ((TextView)findViewById(R.id.name)).setText(customer.getProperty("name").getValue().toString());

                new Thread(new Runnable() {
                    public void run() {
                            ... //get some data, fill some lists and assign some variables...

                        initiateActivity();
                    }
                }).start();

                progressDialog = ProgressDialog.show(this, "Loading", "Please wait...");
}

private void initiateActivity() {
    fillOrderTable();
    initiateGestureview();
    fillCustServiceForCustomer();

    progressDialog.dismiss();
}

但是,一旦我开始更改任何内容或向表中添加数据,我就会收到异常消息"Activity customerapp.customercentral.Overview泄漏了最初在此处添加的窗口com.android.internal.policy.impl.PhoneWindow$DecorView@46d2fce0

But as soon as I start altering anything or adding data to tables I get the exception "Activity customerapp.customercentral.Overview has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@46d2fce0 that was originally added here.

我真的不想在线程中移动初始化方法,因为它们是在我的活动的其他部分重复使用的.

And I really don't wanna move the initation methods inside the thread because they are reused from other part of my activity.

对此有解决方案吗?

非常感谢! :)

尝试过此操作,但随后ProgressDialog不显示...

Tried this but then the ProgressDialog doesn't show...

public void onCreate(Bundle savedInstanceState) {
                ...

                ((ScrollView) findViewById(R.id.contentContainer)).addView(getLayoutInflater().inflate(R.layout.myCustomContentLayout, null));

                ((TextView)findViewById(R.id.name)).setText(customer.getProperty("name").getValue().toString());

                new Thread(new Runnable() {
                    public void run() {
                        runOnUiThread(new Runnable() {
                            public void run() {
                                ... //get some data, fill some lists and assign some variables...

                                initiateActivity();
                            }
                        });
                    }
                }).start();

                progressDialog = ProgressDialog.show(this, "Loading", "Please wait...");
}

这同样适用...

public void onCreate(Bundle savedInstanceState) {
                ...

                ((ScrollView) findViewById(R.id.contentContainer)).addView(getLayoutInflater().inflate(R.layout.myCustomContentLayout, null));

                ((TextView)findViewById(R.id.name)).setText(customer.getProperty("name").getValue().toString());

                new AsyncTask<Boolean, Boolean, Boolean>() {
                    protected void onPreExecute() {
                        progressDialog = ProgressDialog.show(this, "Loading", "Please wait...");
                    }

                    protected Boolean doInBackground(Boolean... unused) {
                        runOnUiThread(new Runnable() {
                            public void run() {
                                ... //get some data, fill some lists and assign some variables...

                                initiateActivity();
                            }
                        });

                        return true;
                    }

                    protected void onPostExecute(Boolean unused) {
                        progressDialog.dismiss();
                    }
                }).execute(true);
}

推荐答案

解决方案:

它可能不是最佳或最漂亮的解决方案,但它可以工作.启动一个新线程,以便在新线程检索,分配和中断主线程时,主线程可以显示一个ProgressDialog,并使用新的可运行线程来启动视图.为了在新线程中的所有活动正在进行时显示ProgressDialog,必须如上所述在主线程中启动ProgressDialog,然后在新线程结束时将其关闭.必须在新线程启动之前立即启动ProgressDialog,并且您要等待的所有内容都必须进入新线程.记住,如果要在等待并显示ProgressDialog时进行UI更改,则必须使用新的runnable中断主线程,这将通过使用runOnUiThread在其run()方法中进行更改.

It might not be the best or the prettiest solution, but it works. Start a new thread so the main thread can display a ProgressDialog while the new thread retrieves, assigns and interrupts the main thread with a new runnable to initiate views. For the ProgressDialog to be shown while all the activity in the new thread is going on, the ProgressDialog must, as mentioned, be started in the main thread and dismissed of course at the end of the new thread. The ProgressDialog must be started as the last thing right before the new thread starts, and everything you wanna wait for must go into the new thread. And remember, if you wanna make UI alterations while waiting and showing ProgressDialog you must interrupt the main thread with a new runnable, which makes the alterations in it's run() method, by using runOnUiThread.

public void onCreate(Bundle savedInstanceState) {
                ...

                ((ScrollView) findViewById(R.id.contentContainer)).addView(getLayoutInflater().inflate(R.layout.myCustomContentLayout, null));

                ((TextView) findViewById(R.id.name)).setText(customer.getProperty("name").getValue().toString());

                final ProgressDialog progressDialog = ProgressDialog.show(this, "Loading", "Please wait...");

                new Thread(new Runnable() {
                    protected void run() {
                        ... //get some data, fill some lists and assign some variables...

                        runOnUiThread(new Runnable() {
                                          public void run() {
                                              initiateActivity();
                                          }
                                      });

                        progressDialog.dismiss();

                        return true;
                    }
                }).start();
}

这篇关于其他线程不会让我更改“活动"布局!安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆