2017-02-19 49 views
0

對不起,我知道這個問題已被問了好幾次,並有很多答案,但沒有一個解決了我的問題。 我打電話給網絡服務和顯示對話框,這工作正常。但我無法解僱progressDialog。雖然同樣的方法是在片段內工作,但是這次我在Activity中使用它。請指出我犯了什麼錯誤。無法解僱progressDialog內活動

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; 
} 
+1

這通常是因爲'progressDialog'不再指向您在調用'dismiss()'時所認爲的實例。而不是'progressDialog'的類字段,在'signupServiceResponse()'中使'最後的ProgressDialog progressDialog = ...'在那裏。另外,請確保你沒有真正結束多個可見的實例。也就是說,確保你不會無意中多次調用'signupServiceResponse()'。 –

+0

只調用signupServiceResponse()一次。我已經像這樣宣佈了progressDialog。 ProgressDialog progressDialog = null。我應該刪除它嗎? – Andrain

+0

您的'progressDialog'變量引用'createProgressDialog'方法返回的'ProgressDialog',而不是'android.app.ProgressDialog'。這就是爲什麼你的dismiss()方法不起作用的原因。 –

回答

1

將您的dialog設爲全局變量,並在onCreate()(如果您在活動中)初始化它。

dialog = new ProgressDialog(mContext); 

添加此方法。

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

最後,而不是調用progressDialog.dismiss()只需撥打dismiss()

0

你必須這樣檢查/寫。

如果(progressDialog!= NULL & & progressDialog.isshowing){

progressDialog.dismiss(); 

}

我使用的,它是工作。

+0

已經嘗試過這種情況,但它也不起作用 – Andrain

+0

檢查是否這樣 – maulik

相關問題