2017-06-13 82 views
-1

我有一個片段,其中,我用後臺線程從服務器加載一些數據和更新使用「runOnUiThread」。但它給我的錯誤「片段未連接到活動」的UI。 我該如何解決這個問題?如何解決沒有附加到Android活動的片段?

代碼: -

AsyncTask.execute(new Runnable() { 
     @Override 
     public void run() { 
      /*Checking whether user is loggedin or not*/ 
      if (loginSessionManager.isLogin()) { 
       /*Creating object of "CServerRequest" class*/ 
       cServerRequest = new CServerRequest(mContext); 
       JSONObject jsonObject = null; 
       try { 
        jsonObject = new JSONObject(); 
        jsonObject.put(CRequestKey.AGENT_CODE, m_szMobileNumber.trim()); 
        jsonObject.put(CRequestKey.PIN, m_szEncryptedPassword.trim()); 
        if (BuildConfig.kDebugInfo) 
         Log.d(TAG, "Request::" + jsonObject); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

       if (isAdded()) { 
        getActivity().runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          m_loading_layout.setVisibility(View.VISIBLE); 

         } 
        }); 
       } 
       String s_szWalletURL = CAPIStorage.IREWARDS_URL + CAPIStorage.WALLET_BALANCE_URL; 
       cServerRequest.postWalletRequest("POST", s_szWalletURL, jsonObject); 

       /*Handling server response and error here*/ 
       cServerRequest.setCallBackListener(new CServerRequest.IResult() { 
        @Override 
        public void notifySuccess(String requestType, JSONObject response) { 
         if (BuildConfig.kDebugInfo) 
          Log.d(TAG, "Response::" + response); 
         try { 
          int nResultCodeFromServer = Integer.parseInt(response.getString(CResponseKey.GENERAL_RESULT_CODE)); 

          if (nResultCodeFromServer == ConstantInt.TRANSACTION_SUCCESS) { 
           if (isAdded()) { 
            getActivity().runOnUiThread(new Runnable() { 
             @Override 
             public void run() { 
              m_loading_layout.setVisibility(View.GONE); 
              m_ErrorLayout.setVisibility(View.GONE); 
              m_walletHeading.setVisibility(View.VISIBLE); 
              m_SuccessLayout.setVisibility(View.VISIBLE); 
             } 

            }); 
           } 

           String s_szWalletBalance = response.getString(CResponseKey.WALLET_BALANCE).trim();// get wallet balance fro response 
           String trimwalletBalance = s_szWalletBalance.substring(0, s_szWalletBalance.indexOf("."));// trim waalet balance from response. 
           CWalletDataModel.getInstance().setS_szWalletBalance(trimwalletBalance);// set wallet balance 
           // showing wallet balance here from ui thread. 
           if (isAdded()) { 
            getActivity().runOnUiThread(new Runnable() { 
             @Override 
             public void run() { 
              if (m_szCountryCode.equalsIgnoreCase(CResponseKey.COUNTRY_INDIA)) { 
               m_currency.setText(getString(string.ruppes_symbol)); 
              } else { 
               m_currency.setText(getString(string.currency_dollar)); 
              } 
              m_points.setText(CWalletDataModel.getInstance().getS_szWalletBalance()); 

              if (m_szCountryCode.equalsIgnoreCase(CResponseKey.COUNTRY_INDIA)) { 
              /*If found indian user then show conversion of points in rupees*/ 
               amountConversionIndia(); 
              } else { 
              /*If found non-indian then will show conversion of points in doller*/ 
               amountConversionInternational(); 
              } 
             } 

            }); 


           } 
           /*Connection lost error*/ 
          } else if (nResultCodeFromServer == ConstantInt.CONNECTION_LOST) { 
           if (isAdded()) { 
            getActivity().runOnUiThread(new Runnable() { 
             @Override 
             public void run() { 

              m_loading_layout.setVisibility(View.GONE); 
              m_ErrorLayout.setVisibility(View.VISIBLE); 
              m_walletHeading.setVisibility(View.GONE); 
              m_ErrorText.setText(getResources().getString(string.connection_lost)); 
              m_ErrorText.setText(getResources().getString(string.retry)); 

             } 
            }); 
           } 
           /*Timed out error from response*/ 
          } else if (nResultCodeFromServer == ConstantInt.TIMED_OUT) { 
           if (isAdded()) { 
            getActivity().runOnUiThread(new Runnable() { 
             @Override 
             public void run() { 

              m_loading_layout.setVisibility(View.GONE); 
              m_ErrorLayout.setVisibility(View.VISIBLE); 
              m_walletHeading.setVisibility(View.GONE); 
              m_ErrorText.setText(getResources().getString(string.connection_timed_out)); 
              m_ErrorBtn.setText(getResources().getString(string.retry_text)); 

             } 

            }); 
           } 
           /*Technical failure response from server*/ 
          } else if (nResultCodeFromServer == ConstantInt.TECHNICAL_FAILURE) { 
           if (isAdded()) { 
            getActivity().runOnUiThread(new Runnable() { 
             @Override 
             public void run() { 

              m_loading_layout.setVisibility(View.GONE); 
              m_ErrorLayout.setVisibility(View.VISIBLE); 
              m_walletHeading.setVisibility(View.GONE); 
              m_ErrorText.setText(getResources().getString(string.technical_failure)); 
              m_ErrorBtn.setText(getResources().getString(string.retry_text)); 

             } 

            }); 
           } 
          } 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 

        @Override 
        public void notifyError(String requestType, VolleyError error) { 
         if (BuildConfig.kDebugInfo) 
          Log.d(TAG, "Error::" + error); 
         if (isAdded()) { 
         /*Time out volley error*/ 
          if (error instanceof TimeoutError) { 

           getActivity().runOnUiThread(new Runnable() { 
            @Override 
            public void run() { 
             m_loading_layout.setVisibility(View.GONE); 
             m_ErrorLayout.setVisibility(View.VISIBLE); 
             m_walletHeading.setVisibility(View.GONE); 
             m_ErrorText.setText(getResources().getString(string.connection_timed_out)); 
             m_ErrorBtn.setText(getResources().getString(string.retry_text)); 
            } 
           }); 
          /*Network volley error*/ 
          } 
         } 
         if (isAdded()) { 

          if (error instanceof NetworkError) { 
           getActivity().runOnUiThread(new Runnable() { 
            @Override 
            public void run() { 
             m_loading_layout.setVisibility(View.GONE); 
             m_ErrorLayout.setVisibility(View.VISIBLE); 
             m_walletHeading.setVisibility(View.GONE); 
             m_ErrorText.setText(getResources().getString(string.unable_to_connect_text)); 
             m_ErrorBtn.setText(getResources().getString(string.retry)); 
            } 
           }); 
          } 
         } 


        } 
       }); 
      } else { 
       if (isAdded()) { 
        getActivity().runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          if (isAdded()) { 
           m_loading_layout.setVisibility(View.GONE); 
           m_ErrorLayout.setVisibility(View.VISIBLE); 
           m_ErrorText.setText(mContext.getResources().getString(string.please_log_into_your_account)); 
           m_ErrorBtn.setText(getResources().getString(string.login_btn_text)); 
          } 
         } 
        }); 
       } 
      } 


     } 
    }); 
} 
+0

請將您的完整活動和片段代碼 – breakline

+0

檢查完整代碼 – Niraj

回答

0

解決這個問題應該取消的onStop您的AsyncTask()。 您還沒有引入內存泄漏的問題。

@Override 
public void onStop() { 
    super.onStop(); 
    mYourAsyncTask.cancel(true); 
}