进度对话框中的选项卡布局的android(Progress Dialog in Tab Layout

2019-10-17 00:53发布

我工作的标签布局和进度对话框。 我想显示进度对话框五秒钟,当用户来到在选项卡的布局和在用户更改标签。

我用下面的相同的代码显示进度在这两个条件对话框。

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

进度对话框工作正常,但标签的内容不会显示。 它显示下面的错误。

“只有创建视图层次原来的线程可以触摸它的意见”

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

}



Answer 2:

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

runOnUiThread(new Runnable() {
            public void run() {
                progressDialog.dismiss();
            }
        });


文章来源: Progress Dialog in Tab Layout in android