Progress Dialog in Tab Layout in android

2019-09-20 07:36发布

问题:

I am working on Tab layout and progress dialog. I want to show progress dialog for five sec when user comes on the tab layout and when user changes the tabs.

I used following same code to show the progress dialog in both conditions.

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

The progress dialog working fine, but contents of the tabs does not display. It is showing following error.

"Only the original thread that created a view hierarchy can touch its views"

回答1:

try this

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

}



回答2:

My guess is it crashes in progressDialog.dismiss() since you're trying to remove from a non UI Thread. In order to prevent this behavior, you should run it in UI/Main Thread like this:

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