ProgressDialog直到函数完成后才显示

时间:2022-08-23 17:55:49

I am trying to show a spinning progress while I load some data, but it isn't showing until after the data is loaded?

我正在尝试在加载一些数据时显示旋转进度,但直到数据加载后才显示?

Here is how I am trying to do this:

以下是我尝试这样做的方法:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen1);

    //Show spinner while data is being loaded
    ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);

    LoadPreferences();
    LoadData();

    //Remove the spinner once all the data has been loaded
    dialog.dismiss();
}

What am I doing wrong?

我究竟做错了什么?

Updated code:

更新的代码:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen1);

//Show spinner while data is being loaded
final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);

// define and run background thread
Thread backgroundThread = new Thread(new Runnable() 
{            
    public void run() 
    {
        // keep sure that this operations
        // are thread-safe!
        Looper.prepare(); //I had to include this to prevent force close error
        LoadPreferences();
        LoadData();

        runOnUiThread(new Runnable() 
        {                    
            public void run() 
            {
                dialog.dismiss();
            }
        });
    }
 });
        backgroundThread.start();
}

This now loads the app and shows the spinner. But my LoadData() function is crashing it when trying to update the UI:

现在加载应用程序并显示微调器。但是我的LoadData()函数在尝试更新UI时崩溃了:

12-27 21:58:12.575: ERROR/AndroidRuntime(357): Uncaught handler: thread Thread-8 exiting due to uncaught exception
12-27 21:58:12.575: ERROR/AndroidRuntime(357): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.view.ViewRoot.checkThread(ViewRoot.java:2683)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.view.ViewRoot.requestLayout(ViewRoot.java:557)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.widget.TableLayout.requestLayout(TableLayout.java:223)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.widget.ScrollView.requestLayout(ScrollView.java:1103)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.view.View.requestLayout(View.java:7918)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.widget.TextView.checkForRelayout(TextView.java:5373)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.widget.TextView.setText(TextView.java:2684)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.widget.TextView.setText(TextView.java:2552)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at android.widget.TextView.setText(TextView.java:2527)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at com.bebetech.helloWorld.helloWorld.UpdateGUI(helloWorld.java:346)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at com.bebetech.helloWorld.helloWorld.LoadData(helloWorld.java:191)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at com.bebetech.helloWorld.helloWorld.$1.run(helloWorld.java:106)
12-27 21:58:12.575: ERROR/AndroidRuntime(357):     at java.lang.Thread.run(Thread.java:1096)

3 个解决方案

#1


5  

Yes, Mitch Wheat is absolutely correct.

是的,Mitch Wheat绝对正确。

Heres what you have to edit:

下面你要编辑的内容:

   public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Show spinner while data is being loaded
        final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);

        Thread thread=new Thread(new Runnable(){

        public void run(){
            LoadPreferences();
            LoadData();
            runOnUiThread(new Runnable(){

                @Override
                public void run() {
                    if(dialog.isShowing())
                        dialog.dismiss();

                }

            });
        }

        });
        thread.start();

}

#2


3  

You should load the data in a background thread:

您应该在后台线程中加载数据:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    final View screen1 = this.findViewById(R.id.screen1);
    this.setContentView(screen1);

    //Show spinner while data is being loaded
    final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);

    // define and run background thread
    Thread backgroundThread = new Thread(new Runnable() {            
        @Override
        public void run() {
            // keep sure that this operations
            // are thread-safe!
            LoadPreferences();
            LoadData();

            runOnUiThread(new Runnable() {                    
                @Override
                public void run() {
                    dialog.dismiss();                        
                }
            })
        }
    });
    backgroundThread.start();
}

#3


1  

I believe you miss this code line, it should be placed just BEFORE the call to setConetntView:

我相信你错过了这个代码行,它应该在调用setConetntView之前放置:

onCreate(){
    ....
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);        
    this.setContentView(screen1);
    ....
}

#1


5  

Yes, Mitch Wheat is absolutely correct.

是的,Mitch Wheat绝对正确。

Heres what you have to edit:

下面你要编辑的内容:

   public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Show spinner while data is being loaded
        final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);

        Thread thread=new Thread(new Runnable(){

        public void run(){
            LoadPreferences();
            LoadData();
            runOnUiThread(new Runnable(){

                @Override
                public void run() {
                    if(dialog.isShowing())
                        dialog.dismiss();

                }

            });
        }

        });
        thread.start();

}

#2


3  

You should load the data in a background thread:

您应该在后台线程中加载数据:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    final View screen1 = this.findViewById(R.id.screen1);
    this.setContentView(screen1);

    //Show spinner while data is being loaded
    final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);

    // define and run background thread
    Thread backgroundThread = new Thread(new Runnable() {            
        @Override
        public void run() {
            // keep sure that this operations
            // are thread-safe!
            LoadPreferences();
            LoadData();

            runOnUiThread(new Runnable() {                    
                @Override
                public void run() {
                    dialog.dismiss();                        
                }
            })
        }
    });
    backgroundThread.start();
}

#3


1  

I believe you miss this code line, it should be placed just BEFORE the call to setConetntView:

我相信你错过了这个代码行,它应该在调用setConetntView之前放置:

onCreate(){
    ....
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);        
    this.setContentView(screen1);
    ....
}