Android progrees dialog error how to solve this?

耗尽温柔 提交于 2019-12-13 07:18:56

问题


I got an error on marshmallow device like below: but below marshmallow OS progress dialog working fine. Error As follow:

E/AndroidRuntime: FATAL EXCEPTION: main
 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
  at com.android.internal.app.AlertController.setupView(AlertController.java:489)
 at com.android.internal.app.AlertController.installContent(AlertController.java:234)
 at android.app.AlertDialog.onCreate(AlertDialog.java:423)
at android.app.ProgressDialog.onCreate(ProgressDialog.java:198)
at android.app.Dialog.dispatchOnCreate(Dialog.java:394)
at android.app.Dialog.show(Dialog.java:295)

My Code of progress dialog is as below:

 private void setUIToWait(boolean wait) {

        if (wait) {
            progressDialog=new ProgressDialog(LoginActivity.this);
            progressDialog.setCancelable(false);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            progressDialog.show();
            progressDialog.setContentView(new ProgressBar(LoginActivity.this));

        } else {
            progressDialog.dismiss();
        }

    }

and I got error in above code and my asynk task where I Actually call

 protected void onPreExecute() {

            setUIToWait(true);
        }

I don't know What is the problem.. It works file till fri on marshmallow but now every time it gives me an error when my app runs and service call with progress dialog in android device.. please suggest me any solution in my code only. Because I already tried all links and their solutions...

EDIT:

if I write below code

 private void setUIToWait(boolean wait) {

                if (wait) {
if(progressDialog==null){
                    progressDialog=new ProgressDialog(LoginActivity.this);
                    progressDialog.setCancelable(false);
                    progressDialog.setCanceledOnTouchOutside(false);
                    progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
                    progressDialog.show();
                    progressDialog.setContentView(new ProgressBar(LoginActivity.this));
        }
                } else {
                    progressDialog.dismiss();
                }

            }

then It will not showing any error and execute successfully but ProgressDialog is not shown now..


回答1:


Add a check for null

else {
           if( progressDialog != null) 
            progressDialog.dismiss();
        }



回答2:


Use this code for hiding progress dialog:

else {
if (dialog != null) {
        if (dialog.isShowing()) {
            Context context = ((ContextWrapper)dialog.getContext()).getBaseContext();
            if (context instanceof Activity) {
                if (!((Activity) context).isFinishing())
                    dialog.dismiss();
            } else
                dialog.dismiss();
        }
        dialog = null;
    }
}


来源:https://stackoverflow.com/questions/39219078/android-progrees-dialog-error-how-to-solve-this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!