@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_home, container, false);
return myFragmentView;
}
我通常有這樣的onCreateView
執行和我所有的代碼是在裏面,由於建議我把和插入我的代碼的其餘部分爲onViewCreated
。我沒有抱怨,但發生了完全相同的事情。當我打開片段活動時,ASyncTask
不會執行。這裏是我的onViewCreated
:執行的AsyncTask作爲fragmentactivity開始(不執行)
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
Requ connReq = new Requ();
connReq.execute(); //go go power rangers
}
這畢竟不是那麼複雜,但由於某種原因沒有啓動我的AsyncTask。它'顯示'ProgressDialog
開始於onPreExecute
並在onPostExecute
解散。所以你可以說它只是不會執行我的doInBackground
我在做這件事情有什麼問題?我只是想要我的ASyncTask執行,因爲我打開片段並加載我的數據。 我真的很感謝幫助,在此先感謝。我在各地搜索,但我真的找不到適當的解決方案,我認爲會有一個。
PS:在的AsyncTask工作得很好,當我執行添加到onClickListener
我的AsyncTask:
private class Requ extends AsyncTask<String, String[], String[]> {
final String pdMessage = getString(R.string.pd_wait);
ProgressDialog pd = new ProgressDialog(getActivity());
@Override
protected void onPreExecute() {
pd.setMessage(pdMessage);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
}
@Override
protected String[] doInBackground(String... params) {
String[] connResult;
final String SipUrlStr = getString(R.string.sip_url);
String bsm = "";
String bst= "";
String hs= "";
String as= "";
try {
JSONTokener SipTokener = new JSONTokener(Sources.httpConnGet(SipUrlStr).toString());
JSONArray SipArray=new JSONArray(SipTokener);
for(int i=0; i<(SipArray.length()); i++)
{
JSONObject json_obj_sip = yeniSipArray.getJSONObject(i);
bsm = json_obj_sip.getString("mt");
bst = json_obj_sip.getString("tt");
hs = json_obj_sip.getString("pta");
as = json_obj_sip.getString("pta2");
}
} catch (Exception e) {
bsm = getString(R.string.def_sip);
bst = getString(R.string.def_sip);
hs = getString(R.string.has);
as = getString(R.string.ass);
}
connRes = new String[]{bsm, bst, hs, as};
return connRes;
}
@Override
protected void onPostExecute(String[] connRes) {
super.onPostExecute(connRes);
res[0] = connRes[0];
res[1] = connRes[1];
res[2] = connRes[2];
res[3] = connRes[3];
res[4] = connRes[4];
res[5] = connRes[5];
res[6] = connRes[6];
res[7] = connRes[7];
res[8] = connRes[8];
res[9] = connRes[9];
res[10] = connRes[10];
res[11] = connRes[11];
pd.dismiss();
}
}
發佈你的asynctask以及 –
@vipulmittal在這裏,你是先生 –
它執行所有正確的,如果'onPreExecute()'被調用。然而,你正在捕捉所有'Exception'並忽略它們。至少在catch塊中添加一些日誌記錄,以便更好地找出錯誤。 – laalto