【发布时间】:2015-06-02 19:31:08
【问题描述】:
在片段中,我使用“AsyncTask”从 URL 检索数据。我的代码的主要目的是访问数据(通过 AsyncTask)并将“JSONArray”传递给片段。 问题是,在片段方面,当我检查应该有结果的变量时,我得到一个错误,说变量为空。
代码如下:
public class MyFragment extends ListFragment {
//this is the variable that should have the result from the AsyncTask
JSONArray myResult = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
(...)
//execute the asyncTask
new GetResult().execute(email, password);
(...)
}
//The AsyncTask
private class GetResult extends AsyncTask<String, Void, JSONArray> {
@Override
protected JSONArray doInBackground(String... params) {
(...)
JSONArray jsonArray = (JSONArray) json.get("customer");
return jsonArray;
}
protected void onPostExecute(JSONArray result){
(...)
//this is where I try to pass the data to the fragment
MyFragment.this.myResult = result;
}
}
}
有人可以帮助我吗?提前谢谢你。
【问题讨论】:
-
你看到 onPostexecute() 中“结果”的价值了吗?当您使用内部类时,您可以直接在 asynctask 中访问 myResult
-
你应该接受对你帮助最大的答案。
标签: android android-fragments android-asynctask