Android 无法在活动内关闭progressDialog

Android 无法在活动内关闭progressDialog,android,progressdialog,Android,Progressdialog,对不起,我知道这个问题已经被问了好几次,有很多答案,但是没有一个解决了我的问题。 我正在调用web服务并显示对话框,该对话框工作正常。但是我不能拒绝这个对话。虽然相同的方法在片段中工作,但这次我在活动中使用它。请告诉我哪里出了错 public void signupServiceResponse(String phNum,String password){ progressDialog = createProgressDialog(this, false); //prog

对不起,我知道这个问题已经被问了好几次,有很多答案,但是没有一个解决了我的问题。 我正在调用web服务并显示对话框,该对话框工作正常。但是我不能拒绝这个对话。虽然相同的方法在片段中工作,但这次我在活动中使用它。请告诉我哪里出了错

    public void signupServiceResponse(String phNum,String password){
    progressDialog = createProgressDialog(this, false);
    //progressDialog.show();
    final ContentServiceCall request = ServiceGenerator.createService(ContentServiceCall.class, "Empty");
    final Call<UserServiceResponse> call = request.signUp(Constants.WS_VERSION,Constants.LOCAL_EN,Constants.PLATFORM, phNum,password);

    call.enqueue(new Callback<UserServiceResponse>() {
        @Override
        public void onResponse(Call<UserServiceResponse> call, final Response<UserServiceResponse> response) {
            if(response!=null && response.isSuccessful())
            {
                if(response.body()!=null && response.body().getResponse()!=null)
                {
                    if(response.body().getResponse().getResponseCode()== Constants.RESPONSE_CODE_SUCCESS) {

                        if(response.body().getUser() != null && response.body().getUserSubscription()!= null && response.body().getUserSubscription() !=null) {

                            userEntity = response.body().getUser();
                            userProfileEntity = response.body().getUserProfile();
                            userSubscriptionEntity = response.body().getUserSubscription();
                            //insert in user table
                            int tableCode = 1; //table code 1 for user table
                            dbHelper.insertUserRegistration(userEntity, userProfileEntity, userSubscriptionEntity, tableCode);
                            dbHelper.close();
                            progressDialog.dismiss();
                            Intent i = new Intent(RegistrationActivity.this, ActivateAccountActivity.class);
                            startActivity(i);
                        }

                    } else if((response.body().getResponse().getResponseCode()== Constants.USERAlREADYEXIST_RESPONSE_CODE_SUCCESS)) {
                        // in case user data is cleared or app is reinstalled
                        boolean userCount = dbHelper.getUserCount();
                        if (userCount) {
                            Intent i = new Intent(RegistrationActivity.this, MainActivity.class);
                            startActivity(i);

                        } else if(!userCount){
                            // if user exist and data is cleared
                            userEntity = response.body().getUser();
                            userProfileEntity = response.body().getUserProfile();
                            userSubscriptionEntity = response.body().getUserSubscription();
                            int tableCode = 1;
                            dbHelper.insertUserRegistration(userEntity, userProfileEntity, userSubscriptionEntity, tableCode);
                            dbHelper.close();
                        }

                    } else if((response.body().getResponse().getResponseCode()== Constants.RESPONSE_CODE_PASSWORD_INCORRECT)){
                        Toast.makeText(RegistrationActivity.this,"Password incorrect",Toast.LENGTH_LONG).show();
                        btnForgetPassword.setVisibility(View.VISIBLE);
                        progressDialog.dismiss();
                    }

                    else {
                    }
                }
                else {
                    // leave it
                }
            }
            else
            {
                // Display proper message
                //Toast.makeText(getActivity(),getString(R.string.error_webservice_response),Toast.LENGTH_LONG).show();
            }
            progressDialog.dismiss();
        }

        @Override
        public void onFailure(Call<UserServiceResponse> call, Throwable t) {

            Log.e("Fail", "Failure");
            Log.e("ERROR", t.getMessage());
                progressDialog.dismiss();
              Toast.makeText(RegistrationActivity.this,getString(R.string.error_internet_connectivity),Toast.LENGTH_LONG).show();
        }
    });
}

public ProgressDialog createProgressDialog(Context mContext, Boolean cancelable) {
    final ProgressDialog dialog = new ProgressDialog(mContext);
    try {
        dialog.show();
    } catch (WindowManager.BadTokenException e) {

    }
    dialog.setCancelable(cancelable);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.setContentView(R.layout.dialog);

    return dialog;
}
public void signupservice响应(字符串phNum,字符串密码){
progressDialog=createProgressDialog(此为false);
//progressDialog.show();
最终ContentServiceCall请求=ServiceGenerator.createService(ContentServiceCall.class,“空”);
最终调用=request.signUp(Constants.WS_VERSION、Constants.LOCAL_EN、Constants.PLATFORM、phNum、密码);
call.enqueue(新回调(){
@凌驾
公共void onResponse(呼叫、最终响应){
if(response!=null&&response.issusccessful())
{
if(response.body()!=null&&response.body().getResponse()!=null)
{
if(response.body().getResponse().getResponseCode()==Constants.response\u CODE\u SUCCESS){
if(response.body().getUser()!=null和response.body().getUserSubscription()!=null和response.body().getUserSubscription()!=null){
userEntity=response.body().getUser();
userProfileEntity=response.body().getUserProfile();
userSubscriptionEntity=response.body().getUserSubscription();
//在用户表中插入
int tableCode=1;//用户表的表代码1
insertUserRegistration(userEntity、userProfileEntity、userSubscriptionEntity、tableCode);
dbHelper.close();
progressDialog.disclose();
意向i=新意向(RegistrationActivity.this,ActivateAccountActivity.class);
星触觉(i);
}
}else if((response.body().getResponse().getResponseCode()==Constants.USERAlREADYEXIST\u response\u CODE\u SUCCESS)){
//如果清除用户数据或重新安装应用程序
boolean userCount=dbHelper.getUserCount();
if(userCount){
意向i=新意向(RegistrationActivity.this,MainActivity.class);
星触觉(i);
}如果(!userCount){
//如果用户存在且数据已清除
userEntity=response.body().getUser();
userProfileEntity=response.body().getUserProfile();
userSubscriptionEntity=response.body().getUserSubscription();
int tableCode=1;
insertUserRegistration(userEntity、userProfileEntity、userSubscriptionEntity、tableCode);
dbHelper.close();
}
}else if((response.body().getResponse().getResponseCode()==常量。响应\代码\密码\不正确)){
Toast.makeText(RegistrationActivity.this,“密码不正确”,Toast.LENGTH_LONG.show();
btnformetpassword.setVisibility(View.VISIBLE);
progressDialog.disclose();
}
否则{
}
}
否则{
//别管它
}
}
其他的
{
//显示正确的信息
//Toast.makeText(getActivity()、getString(R.string.error\u webservice\u response)、Toast.LENGTH\u LONG.show();
}
progressDialog.disclose();
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“失败”、“失败”);
Log.e(“ERROR”,t.getMessage());
progressDialog.disclose();
Toast.makeText(RegistrationActivity.this、getString(R.string.error\u internet\u connectivity)、Toast.LENGTH\u LONG.show();
}
});
}
public ProgressDialog createProgressDialog(上下文mContext,布尔可取消){
最终进度对话框=新建进度对话框(mContext);
试一试{
dialog.show();
}捕获(WindowManager.BadTokene异常){
}
对话框。可设置可取消(可取消);
dialog.getWindow().setBackgroundDrawable(新的ColorDrawable(Color.TRANSPARENT));
setContentView(R.layout.dialog);
返回对话框;
}

你必须这样检查/书写

if(progressDialog!=null&& progressDialog.isshowing){

}


我正在使用,它正在工作。

将您的
对话框
设置为全局变量,并在
onCreate()
中初始化它(如果您在活动中)

添加此方法

public ProgressDialog dismiss() {
    if(dialog != null) {
        dialog.dismiss();
    }
}

最后,不要调用
progressDialog.disease()
只需调用
disease()

,这通常是因为
progressDialog
不再指向调用
disease()
时您认为它会指向的实例。将
final progressDialog progressDialog=…
置于
signupServiceResponse()
中,而不是
progressDialog
的类字段。此外,请确保您实际上没有以多个可见实例结束。也就是说,确保您没有无意中多次调用
signupServiceResponse()
。只调用signupServiceResponse()一次。我已经这样宣布了。ProgressDialog=null。我应该删除它吗?您的
progressDialog
变量引用方法
createProgressDialog
返回的
progressDialog
,而不是

dialog = new ProgressDialog(mContext);
public ProgressDialog dismiss() {
    if(dialog != null) {
        dialog.dismiss();
    }
}