进度对话框从不显示

时间:2018-05-03 05:00:25

标签: android fragment progressdialog

我正在尝试在单击列表项时显示进度对话框,但进度对话框从不显示。

令人惊讶的是,mProgressdialog.isShowing()是真的。如果任何人遇到类似的问题,请提供一些问题的见解。

adapter.setOnItemClickListener(new Adapter.ListClickListener() {
                        @Override
                        public void onItemClick(int position, View v) {



                                    //Some computations done.

                                    if (ProjectsFragment.this.getActivity() != null) {

                                        UniversalProgressDialog.showSimpleProgressDialog(getActivity(),"Loading","Loading...",false);

          android.support.v4.app.FragmentManager fragmentManager = ProjectsFragment.this.getActivity().getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction ft = fragmentManager.beginTransaction();
                    ft.replace(R.id.sample_content_fragment, new AgendaFragment(), "AGENDA");
                    ft.commit();



                                    }
                                }
                            }
                        }
                        });

UniversalProgressDialog的源代码: -

    public class UniversalProgressDialog {

        private static ProgressDialog mProgressDialog;


        public static void showSimpleProgressDialog(Context context, String title,
                                                    String msg, boolean isCancelable) {
            try {
                if (mProgressDialog == null) {
                    mProgressDialog = ProgressDialog.show(context, title, msg);
                    mProgressDialog.setCancelable(false);
                    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    mProgressDialog.setIndeterminate(true);
                    mProgressDialog.setCanceledOnTouchOutside(false);
                }

                if(mProgressDialog.isShowing()){
                    mProgressDialog.dismiss();
                }

                if (!mProgressDialog.isShowing()) {
                    mProgressDialog.show();
                }

            } catch (IllegalArgumentException ie) {
                ie.printStackTrace();
            } catch (RuntimeException re) {
                re.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public static void removeSimpleProgressDialog() {
            try {
                if (mProgressDialog != null) {
                    if (mProgressDialog.isShowing()) {
                        mProgressDialog.dismiss();
                        mProgressDialog = null;
                    }
                }
            } catch (IllegalArgumentException ie) {
                ie.printStackTrace();

            } catch (RuntimeException re) {
                re.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }


    }

我尝试了什么?

尝试将显示代码的进度对话框移动到

尝试将片段转换代码移动到另一个线程,如下所示: -

 private class PerformFragmentTransition extends Thread {


        public PerformFragmentTransition() {
        }

        @Override
        public void run() {

            android.support.v4.app.FragmentManager fragmentManager = ProjectsFragment.this.getActivity().getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction ft = fragmentManager.beginTransaction();
            ft.replace(R.id.sample_content_fragment, new AgendaFragment(), "AGENDA");
            ft.commit();
        }
    }

也尝试过: -

getActivity().runOnUiThread(new Runnable(){
                                public void run() {
                                    UniversalProgressDialog.showSimpleProgressDialog(getActivity(),"Loading","Loading...",false);

                                }
                            });

也尝试过: -

根据其中一个提交,我注释掉了片段转换代码并显示了进度对话框。

如果可以提供一些如何解决问题的线索,  以及更多信息: -  我将在加载后删除下一个片段中的进度对话框。

1 个答案:

答案 0 :(得分:0)

mProgressDialog = ProgressDialog.show(context, title, msg);

使用以下代码替换上面的代码:

 mProgressDialog = new ProgressDialog(context);
        mProgressDialog.setTitle(title);
        mProgressDialog.setMessage(msg);

问题是您在代码中创建对话框时显示对话框 在为其设置所需属性之前。并请删除try catch块,这样你就会得到错误实际发生的错误。并且可以在控制台中看到