我有一个包含MainActivity
的应用,该活动包含发送服务器请求的fragment
,在此期间我显示ProgressDialog
。但是当显示进度对话框时,如果我从MainActivity
移动到另一个活动,则会给我错误"Fragment not attached to activity"
。
我该如何解决这个问题?
代码: -
/*This method send request to server for more deals*/
private void loadmoreData() {
if (mSwipeRefresh.isRefreshing()) {
mSwipeRefresh.setRefreshing(false);
}
try {
String json;
// 3. build jsonObject
final JSONObject jsonObject = new JSONObject();// making object of Jsons.
jsonObject.put(ServerRequestKeyStorage.s_szAGENT_CODE, m_szMobileNumber);// put mobile number
jsonObject.put(ServerRequestKeyStorage.s_szPASSWORD, m_szEncryptedPassword);// put password
jsonObject.put(ServerRequestKeyStorage.s_szRECORD_COUNT, sz_RecordCount);// put record count
jsonObject.put(ServerRequestKeyStorage.s_szLAST_COUNT, sz_LastCount);// put last count
Log.d("CAppList:", sz_RecordCount);
Log.d("Capplist:", sz_LastCount);
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();// convert Json object to string
Log.i(TAG, "Server Request:-" + json);
m_Dialog = DialogUtils.showProgressDialog(getActivity(), getString(R.string.loading_more_deals));
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
final String imgPath = APIStorage.IREWARDS_URL + APIStorage.s_szImagePath;
final String m_DealListingURL = APIStorage.IREWARDS_URL + APIStorage.s_szDEALLISTING_URL;
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, m_DealListingURL, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e(TAG, "Server Response:-" + response);
Activity activity = getActivity();
if (activity!=null&&isAdded()){
m_Dialog.dismiss();
}
try {
int nResultCodeFromServer = Integer.parseInt(response.getString(ServerResponseStorage.s_szRESULT_CODE));
if (nResultCodeFromServer == CStaticVar.m_kTRANSACTION_SUCCESS) {
// Select the last row so it will scroll into view...
JSONArray posts = response.optJSONArray(ServerResponseStorage.s_szDEAL_ARRAY);// GETTING DEAL LIST
for (int i = 0; i < posts.length(); i++) {
try {
JSONObject post = posts.getJSONObject(i);// GETTING DEAL AT POSITION AT I
item = new CDealAppDatastorage();// object create of DealAppdatastorage
item.setM_szHeaderText(post.getString(ServerResponseStorage.s_szDEAL_NAME));//getting deal name
item.setM_szsubHeaderText(post.getString(ServerResponseStorage.s_szDEAL_CODE));// getting deal code
item.setM_szDealValue(post.getString(ServerResponseStorage.s_szDEAL_VAlUE));
item.setM_szDetails(post.getString(ServerResponseStorage.s_szDEAL_DETAILS));
String logo = post.getString(ServerResponseStorage.s_szDEAL_LOGO);
item.setM_szLogoPath(imgPath + logo);
Log.e(TAG, "Logo Path::" + item.getM_szLogoPath());
if (!s_oDataset.contains(item)) {
s_oDataset.add(item);
}
} catch (Exception e) {
e.printStackTrace();
}
}
m_oAdapter.notifyDataSetChanged();
arrayCount = posts.length();// finding length of deals coming in response from server.
// read stored value from shared preference
int n_oLastCountLength = m_oPreferenceHelper.getIntPreference(LAST_COUNT_LENGTH);
int accumulateLastCount = n_oLastCountLength + arrayCount;
/*Here we are saving deal length in shared preference*/
// save incremental length
m_oPreferenceHelper.saveIntegerValue(LAST_COUNT_LENGTH, accumulateLastCount);
// m_ListView.removeFooterView(mFooter);
m_ListView.setSelection(m_oAdapter.getCount() - posts.length());
}
if (nResultCodeFromServer == CStaticVar.m_kCONNECTION_LOST) {//server based conditions
CSnackBar.showSnackBarError(m_MainLayout, getString(R.string.connection_not_available), getActivity());
// m_ListView.removeFooterView(mFooter);
} else if (nResultCodeFromServer == CStaticVar.m_kDEAL_NOT_FOUND) {// serevr based conditions .....
CSnackBar.showSnackBarError(m_MainLayout, getString(R.string.no_more_deals), getActivity());
// //*Counting loading footer*/
// if (m_ListView.getFooterViewsCount() != 0) {
// m_ListView.removeFooterView(mFooter);
// }
} else if (nResultCodeFromServer == CStaticVar.m_kTECHNICAL_FAILURE) {
CSnackBar.showSnackBarError(m_MainLayout, getString(R.string.technical_failure), getActivity());
} else if (nResultCodeFromServer == CStaticVar.m_kALREADY_AVAIL_BENEFIT) {
CSnackBar.showSnackBarError(m_MainLayout, getString(R.string.already_avail_deals), getActivity());
} else if (nResultCodeFromServer == CStaticVar.m_kTIMED_OUT) {
CSnackBar.showSnackBarError(m_MainLayout, getString(R.string.times_out), getActivity());
// m_ListView.removeFooterView(mFooter);
} else if (nResultCodeFromServer == CStaticVar.m_kERROR_IN_DB) {
CSnackBar.showSnackBarError(m_MainLayout, "Something happened wrong in DB. Please contact support@starxsoft.com", getActivity());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Server Error::" + error);
Activity activity = getActivity();
if (activity!=null&&isAdded()){
m_Dialog.dismiss();
}
if (error instanceof TimeoutError) {
CSnackBar.showSnackBarError(m_MainLayout, getString(R.string.connection_timed_out), getActivity());
} else if (error instanceof NetworkError) {
CSnackBar.showSnackBarError(m_MainLayout, getString(R.string.no_internet_warning), getActivity());
}
}
});
requestQueue.add(jsonObjectRequest);
} catch (JSONException e) {
e.printStackTrace();
}
}
在解雇和显示对话期间,这给了我错误。