简体   繁体   English

Android无法显示ProgressDialog

[英]Android cannot show ProgressDialog

SITUATION 情况

I have been reading a lot of stuff about this on google but I still can't solve my problem. 我一直在谷歌上阅读很多关于这个的东西,但我仍然无法解决我的问题。 I have an app that, once you press a button, has to execute a lot of tasks before showing the output. 我有一个应用程序,一旦按下按钮,就必须在显示输出之前执行许多任务。 I want to display a progress dialog like this: 我想显示一个这样的进度对话框:

在此输入图像描述

Very easy, the circle that is moving on the left and the text "Calculating results..." on the left. 非常简单,左侧移动的圆圈和左侧的“计算结果...”文字。


PROBLEM 问题

I have the following code: 我有以下代码:

public class fragmentMetodoTangenti extends Fragment {

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) { 

  // Inflate the layout for this fragment
  final View view = inflater.inflate(R.layout.fragment_metodo_tangenti, container, false);

  Button button = (Button) view.findViewById(R.id.button1);
  button.setOnClickListener(new View.OnClickListener() {

   @Override
        public void onClick(View v) {


         try {

             loading = new ProgressDialog(view.getContext());
             loading.setCancelable(true);
             loading.setMessage("Calculating results...");
             loading.setProgressStyle(ProgressDialog.STYLE_SPINNER);
             loading.show();

          //other "heavy" code here (a lot of for and while loops)

             loading.dismiss();

         } catch (Exception e) {
          //catching the error
         }


  });

  //return the view
  return view;

 }

}

The code works perfectly because I see the correct output at the end (some numbers and letters). 代码完美无缺,因为我在末尾看到了正确的输出(一些数字和字母)。 The only problem is that when I click the button I cannot see the Progress Dialog with the spinner inside. 唯一的问题是,当我单击按钮时,我看不到内部有微调器的进度对话框。

I think that the problem could be on new ProgressDialog(view.getContext()); 我认为问题可能在于new ProgressDialog(view.getContext()); but I am not sure. 但我不确定。 I am using the latest version of Android Studio. 我使用的是最新版本的Android Studio。

How could I solve this? 我该怎么解决这个问题?

LOG HERE 在这里记录

You should move your heavy code in an AsynkTask and create the progressDialog in the onPreExecute() callback and dismiss it in the onPostExecute() callback. 您应该在AsynkTask中移动繁重的代码并在onPreExecute()回调中创建progressDialog并在onPreExecute()回调中将其onPostExecute()

More info here: https://developer.android.com/reference/android/os/AsyncTask.html 更多信息: https//developer.android.com/reference/android/os/AsyncTask.html

The OnClick callback is called on the UI thread, in order to keep the UI smooth you should not do "Heavy" tasks on this thread. 在UI线程上调用OnClick回调,为了保持UI平滑,您不应该在此线程上执行“重”任务。

This may be overkill for this case, but you should take a look to https://github.com/ReactiveX/RxAndroid . 对于这种情况,这可能是过度的,但你应该看看https://github.com/ReactiveX/RxAndroid Trust me, there are a lot of benefit of using RxJava/RxAndroid to work, in this case you could solve the problem by doing: 相信我,使用RxJava / RxAndroid工作有很多好处,在这种情况下,您可以通过以下方式解决问题:

public void OnClick(View v) {
    Observable.defer(() -> heavyTask())
              .observeOn(Schedulers.computation())
              .subscribeOn(AndroidSchedulers.mainThread())
              .doOnSubscribe(() -> showProgressBar())
              .doOnCompleted(() -> hideProgressBar())
              .subscribe();
}

It's hard at the start, but you end up with really neat code. 一开始很难,但最终会得到非常简洁的代码。

Alternatively you can take a look at AsyncTask class which is the standard solution for these kind of problems. 或者,您可以查看AsyncTask类,它是这类问题的标准解决方案。 You can find a sample here Download a file with Android, and showing the progress in a ProgressDialog 您可以在此处找到示例使用Android下载文件,并在ProgressDialog中显示进度

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

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