简体   繁体   English

加载图片时未显示进度对话框

[英]Progress dialog not showing while loading pictures

I have to load several pictures into the main thread and I want to show a loading dialog while this is happening, but somehow I can't get to show the progress dialog... 我必须将几张图片加载到主线程中,并且要在发生这种情况时显示一个加载对话框,但是以某种方式我无法显示进度对话框...

My code below: 我的代码如下:

private ProgressDialog progress;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.background);

    progress = ProgressDialog.show(this, "",getString(R.string.background_loading), true);

    LoadBackgrounds loadB = new LoadBackgrounds();
    new Thread(loadB).start();

}

public class LoadBackgrounds implements Runnable {
     @Override
     public void run() {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                loadPictures();
                progress.dismiss();
            }
        });
     }
 }

loadPictures() must be called inside runOnUiThread because it modifies the views. 必须在runOnUiThread内部调用loadPictures()因为它会修改视图。 So, why isn't the progress dialog showing while the pictures are loading? 那么,为什么在加载图片时不显示进度对话框?

I am not 100% sure but here is my guess: 我不确定100%,但这是我的猜测:

  1. You create a Thread that should run separately from the UIThread but inside that you call runOnUiThread() which merges this thread with the UIThread. 您创建了一个应与UIThread分开运行的线程,但在其中调用runOnUiThread()将该线程与UIThread合并。
  2. You create a runnable to run in a thread that creates a runnable that runs on another thread. 您创建了一个在线程中运行的可运行对象,而该线程又在另一个线程上运行了。 I guess you should understand the nonsense of that 我想你应该明白那个废话
  3. Your loadPictures() will probably slow down the UIThread which could be the reason that you do not see the progress dialog in the first place and as soon as the loading is done you dismiss it. 您的loadPictures()可能会减慢UIThread的速度,这可能是您首先看不到进度对话框,并且在完成加载后便将其关闭的原因。
  4. I also guess that you get either an ANR Dialog (Application not responding) or you see a lot of Skipped X frames! The application may be doing too much work on its main thread. 我还猜测您会收到一个ANR对话框(应用程序未响应),或者看到很多“ Skipped X frames! The application may be doing too much work on its main thread. Skipped X frames! The application may be doing too much work on its main thread. messages in your logcat output. logcat输出中的消息。

My advice and depending on what your loadPictures() method actually does: Use an AsyncTask or, when you access a server to get the pictures, check libraries like Volley or RetroFit and use them to get the data. 我的建议取决于您的loadPictures()方法的实际作用:使用AsyncTask,或者在访问服务器以获取图片时,检查VolleyRetroFit等库并使用它们获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM