0

我有列表视图的问题。每次我运行代码时,如果我按下 listview - 程序会因代码而崩溃:

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)]

这里的代码片段:

public void getProfileInformation() {
    frienders();
    System.out.println("STORE" + store);
    String fqlQuery = "SELECT status_id, uid , message FROM status WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())";
    Bundle params = new Bundle();
    params.putString("q", fqlQuery);
    params.putString("access_token", facebook.getAccessToken());
    Session session = Session.getActiveSession();
    Request request = new Request(session, "/fql", params, HttpMethod.GET, new Request.Callback() {
        public void onCompleted(Response response) {

            GraphObject graphObject = response.getGraphObject();
            System.out.println(response.toString());
            if (graphObject != null) {
                if (graphObject.getProperty("data") != null) {
                    try {
                        String arry = graphObject.getProperty("data").toString();
                        JSONArray jsonNArray = new JSONArray(arry);
                        for (int i = 0; i < jsonNArray.length(); i++) {
                            JSONObject jsonObject = jsonNArray.getJSONObject(i);
                            String uid = jsonObject.getString("uid");
                            String status_id = jsonObject.getString("status_id");
                            String message = jsonObject.getString("message");
                            Log.i("Entry", "uid: " + uid + ", \nstatus: " + status_id + ", \nmessage: " + message);
                            final HashMap<String, String> map = new HashMap<String, String>();
                            name = store.get(uid);
                            map.put("name", name);
                            map.put("status_id", status_id);
                            map.put("message", message);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    contactList.add(map);
                                }
                            });
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    });
    Request.executeBatchAsync(request);
    System.out.println("aaaa" + contactList);
    ListAdapter adapter = new SimpleAdapter(this, contactList, R.layout.list_item, new String[] { "name", "status_id", "message" }, new int[] { R.id.name,
            R.id.email, R.id.mobile });
    setListAdapter(adapter);
    ListView lv = getListView();
    lv.requestLayout();
    ((BaseAdapter) adapter).notifyDataSetChanged();
}

你能给我一些建议吗?谢谢!

4

1 回答 1

0
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.

您正在执行 UI 操作 ( notifyDataSetChanged ()) non-UI thread,因此您得到了该异常。在主线程 ( UI thread) 中执行 UI 操作。

尝试这个:

         runOnUiThread(new Runnable() 
         {
            @Override
                public void run() {                                                
                   contactList.add(map);
                   your_adapter.notifyDataSetChanged ();
          }});
于 2013-08-26T18:58:21.187 回答