【问题标题】:java.lang.IllegalStateException after click on listview单击列表视图后的 java.lang.IllegalStateException
【发布时间】:2013-08-26 18:46:17
【问题描述】:

我对列表视图有疑问。每次我运行代码时,如果我按下 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();
}

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

【问题讨论】:

  • 您能否格式化您的代码并仅发布相关代码。你的异常说你正在从 doInbackground 访问/修改 ui
  • 该错误消息实际上非常冗长。它非常详细地解释了哪里出了问题,并告诉您如何解决它。除此之外,您的问题描述与您的代码 sn-p 不匹配。你说当你“按下” ListView 时它会崩溃(点击项目?)。我在那里看不到任何触摸或单击事件处理程序。回顾一下:您修改底层适配器数据而不通知 ListView 有关更改。此外,在加载数据之前调用 notifyDataSetChanged 也没有多大意义。

标签: android listview exception


【解决方案1】:
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.

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

试试这个:

         runOnUiThread(new Runnable() 
         {
            @Override
                public void run() {                                                
                   contactList.add(map);
                   your_adapter.notifyDataSetChanged ();
          }});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    相关资源
    最近更新 更多