如何解决未附加到android中的活动的片段?

时间:2017-06-13 04:08:19

标签: android android-fragments

我有一个片段,其中,我使用后台线程从服务器加载一些数据并使用“runOnUiThread”更新UI。但是它给出了错误“片段未附加到活动”。 我该如何解决这个问题?

代码: -

 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));
                            }
                        }
                    });
                }
            }


        }
    });
}

1 个答案:

答案 0 :(得分:0)

此问题的解决方案应该取消onStop()中的asynctask。 你也没有介绍内存泄漏的问题。

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