我正在使用带有操作栏sherlock碎片的导航抽屉。我有一个主要活动并从那里调用四个碎片。在其中一个片段中,需要显示产品列表。我能够成功获取和解析json数据。但是,无法在我的布局中显示列表。在logcat中,直到'In thread'显示出来。之后,应用程序崩溃。
class LoadEnq extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Listing Enquiries ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting Albums JSON
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("TOKEN", "INBOX-S!U@B#O$"));
params.add(new BasicNameValuePair("ACTION", "inbox"));
params.add(new BasicNameValuePair("MODID", "IMOB"));
params.add(new BasicNameValuePair("GLID", "423104"));
params.add(new BasicNameValuePair("FOLDER", "1"));
params.add(new BasicNameValuePair("FROM", "0"));
params.add(new BasicNameValuePair("TO", "500"));
// getting JSON string from URL
String json = jsonParser.makeHttpRequest(URL_ENQ, "POST",
params);
// Check your log cat for JSON reponse
Log.d("Enq JSON: ", "> " + json);
try {
System.out.println("in try");
enq = new JSONArray(json);
if (enq != null) {
// looping through All albums
for (int i = 0; i < enq.length(); i++) {
JSONObject c = enq.getJSONObject(i);
// Storing each json item values in variable
String qid = c.getString(TAG_QID);
String qtype = c.getString(TAG_QTYPE);
String name = c.getString(TAG_SNAME);
String email = c.getString(TAG_SEMAIL);
String subject = c.getString(TAG_SUBJECT);
System.out.println("QID" + qid);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_QID, qid);
map.put(TAG_QTYPE, qtype);
map.put(TAG_SNAME, name);
map.put(TAG_SEMAIL, email);
map.put(TAG_SUBJECT, subject);
// adding HashList to ArrayList
enqList.add(map);
}
}else{
Log.d("Enquries: ", "null");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
System.out.println("post");
// dismiss the dialog after getting all albums
pDialog.dismiss();
System.out.println("dismiss");
// updating UI from Background Thread
//getActivity().runOnUiThread(new Runnable() {
//public void run() {
System.out.println("in thread");
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
getActivity(), enqList,
R.layout.list_item_albums, new String[] { TAG_QID,TAG_QTYPE,
TAG_SNAME, TAG_SEMAIL,TAG_SUBJECT }, new int[] {
R.id.query_id,R.id.qtype ,R.id.name,R.id.email, R.id.subject });
//adapter.notifyDataSetChanged();
// updating listview
setListAdapter(adapter);
//}
//});
}
}
}
堆栈跟踪:
07-09 12:21:02.374: E/AndroidRuntime(482): FATAL EXCEPTION: main
07-09 12:21:02.374: E/AndroidRuntime(482): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(16908298, class android.widget.ListView) with Adapter(class android.widget.SimpleAdapter)]
07-09 12:21:02.374: E/AndroidRuntime(482): at android.widget.ListView.layoutChildren(ListView.java:1492)
07-09 12:21:02.374: E/AndroidRuntime(482): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
07-09 12:21:02.374: E/AndroidRuntime(482): at android.view.View.layout(View.java:7035)
07-09 12:21:02.374: E/AndroidRuntime(482): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
答案 0 :(得分:1)
你应该使用这样的东西
public class YourFragment extends SherlockListFragment{
private ListAdapter adapter;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new YourTask().execute();
}
public class YourTask extends AsyncTask<Object, Void, List<Map<String,String>>> {
@Override
protected List<Map<String,String>> doInBackground(Object... objects) {
List<Map<String,String>> list = new LinkedList<Map<String, String>>();
for (int i = 0; i < enq.length(); i++) {
// ... your json
list.add(map);
}
return list;
}
protected void onPostExecute(List<Map<String,String>> result) {
if (pDialog.isShowing())
pDialog.dismiss();
if (YourFragment.this.isAdded()){
adapter = new SimpleAdapter(
getActivity(), result,
R.layout.list_item_albums, new String[] { TAG_QID,TAG_QTYPE,
TAG_SNAME, TAG_SEMAIL,TAG_SUBJECT }, new int[] {
R.id.query_id,R.id.qtype ,R.id.name,R.id.email, R.id.subject });
setListAdapter(adapter);
}
}
};
}