其他线程不会让我改变活动布局!? Android的

时间:2012-03-14 09:04:23

标签: android multithreading android-activity progressdialog alter

SOLUTION:

它可能不是最好或最漂亮的解决方案,但它有效。启动一个新线程,以便主线程可以显示ProgressDialog,同时新线程检索,分配和中断主线程,并使用新的runnable启动视图。为了在新线程中的所有活动进行时显示ProgressDialog,如上所述,ProgressDialog必须在主线程中启动,当然在新线程结束时被解雇。 ProgressDialog必须在新线程启动之前作为最后一件事启动,并且您要等待的所有内容必须进入新线程。请记住,如果您想在等待并显示ProgressDialog时进行UI更改,则必须使用新的runnable中断主线程,这将使用run()对其进行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();
}

原始帖子:

大家好,非常感谢您的阅读。 :)

是否真的无法从与主UI线程不同的线程更改活动布局?

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

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,最初是在这里添加的

我真的不想在线程中移动启动方法,因为它们是从我活动的其他部分重用的。

有解决方法吗?

非常感谢! :)

修改

试过这个但是ProgressDialog没有显示......

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

2 个答案:

答案 0 :(得分:0)

可以启动从另一个线程更改布局,但所有作业都必须在UI线程中完成。为此,您可以使用Activity.runOnUiThread()View.post()View.postDelayed()方法,也可以使用Handler

文章Painless Threading包含有关此任务的更多详细信息。

答案 1 :(得分:0)

<强> SOLUTION:

它可能不是最好或最漂亮的解决方案,但它有效。启动一个新线程,以便主线程可以显示ProgressDialog,同时新线程检索,分配和中断主线程,并使用新的runnable启动视图。为了在新线程中的所有活动进行时显示ProgressDialog,如上所述,ProgressDialog必须在主线程中启动,当然在新线程结束时被解雇。 ProgressDialog必须在新线程启动之前作为最后一件事启动,并且您要等待的所有内容必须进入新线程。请记住,如果您想在等待并显示ProgressDialog时进行UI更改,则必须使用新的runnable中断主线程,这将使用run()对其进行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();
}
相关问题