0

我正在处理选项卡布局和进度对话框。当用户进入选项卡布局以及用户更改选项卡时,我想显示五秒钟的进度对话框。

我使用以下相同的代码来显示两种情况下的进度对话框。

public class TabLayoutActivity extends TabActivity {
    ProgressDialog progressDialog;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tablayoutview);
        progressDialog = ProgressDialog.show(TabLayoutActivity.this, "",
                "Loading. Please wait...");
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(5000);
                    TabHost tabHost = getTabHost();
                    TabHost.TabSpec tabSpec;
                    Intent intent = new Intent().setClass(
                            getApplicationContext(), FirstTabActivity.class);
                    tabSpec = tabHost.newTabSpec("first").setIndicator("First")
                            .setContent(intent);
                    tabHost.addTab(tabSpec);

                    intent = new Intent().setClass(getApplicationContext(),
                            SecondTabActivity.class);
                    tabSpec = tabHost.newTabSpec("second")
                            .setIndicator("Second").setContent(intent);
                    tabHost.addTab(tabSpec);
                } catch (Exception e) {
                    Log.e("tag", e.getMessage());
                }
                progressDialog.dismiss();
            }
        }.start();
    }
}

进度对话框工作正常,但选项卡的内容不显示。它显示以下错误。

“只有创建视图层次结构的原始线程才能触及它的视图”

4

2 回答 2

1

试试这个

public class TabLayoutActivity extends TabActivity {
ProgressDialog progressDialog;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tablayoutview);
    progressDialog = ProgressDialog.show(TabLayoutActivity.this, "",
            "Loading. Please wait...");
    new Thread() {
        public void run() {
            try {
                Thread.sleep(5000);
 TabLayoutActivity .this.runOnUiThread(new Runnable() {

    @Override
    public void run() {
        TabHost tabHost = getTabHost();
                TabHost.TabSpec tabSpec;
                Intent intent = new Intent().setClass(
                        getApplicationContext(), FirstTabActivity.class);
                tabSpec = tabHost.newTabSpec("first").setIndicator("First")
                        .setContent(intent);
                tabHost.addTab(tabSpec);

                intent = new Intent().setClass(getApplicationContext(),
                        SecondTabActivity.class);
                tabSpec = tabHost.newTabSpec("second")
                        .setIndicator("Second").setContent(intent);
                tabHost.addTab(tabSpec);
              progressDialog.dismiss();
    }
});

            } catch (Exception e) {
                Log.e("tag", e.getMessage());
            }

        }
    }.start();
}

}

于 2012-07-18T10:37:36.497 回答
0

我的猜测是它崩溃了,progressDialog.dismiss()因为您试图从非 UI 线程中删除。为了防止这种行为,您应该像这样在 UI/主线程中运行它:

runOnUiThread(new Runnable() {
            public void run() {
                progressDialog.dismiss();
            }
        });
于 2012-07-18T10:37:02.620 回答