【问题标题】:ProgressDialog not showing until after function finishesProgressDialog 直到函数完成后才显示
【发布时间】:2010-12-26 09:10:52
【问题描述】:

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

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

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

我做错了什么?

更新代码:

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

这会加载应用并显示微调器。但是我的 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)

【问题讨论】:

  • 你需要允许UI线程重绘....

标签: android


【解决方案1】:

是的,米奇小麦是绝对正确的。

以下是您必须编辑的内容:

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

}

【讨论】:

  • 这个答案看起来和 Marcel J. Kloubert 的完全一样。
  • 我们同时提交了我们的答案。
  • 好吧,我没有运气。我必须删除@Override 才能使其编译正常。在加载时它强制关闭。错误是:12-27 21:48:08.724: ERROR/AndroidRuntime(277): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  • @Overrides 是由过时的 Java 编译器引起的......代码对我来说非常完美。
【解决方案2】:

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

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

【讨论】:

  • 这个答案看起来和 Shardul 的完全一样。
  • 我知道,但我快了几分钟 hehehe ;-)
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多