2013-08-26 47 views
0

我有问题与listview。我每次运行的代码,如果我按列表视图 - 程序与代码崩溃:点击列表视图后的java.lang.IllegalStateException

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(); 
} 

您能否提供我什么事?谢谢!

+0

可以格式化你的代码,并张贴只有相关的代码。你的例外说你是从doInbackground访问/修改ui – Raghunandan

+0

这个错误信息实际上很冗长。它详细解释出了什么问题,并告诉你如何解决问题。除此之外,您的问题描述与您的代码段不匹配。你说当你“按下”ListView(在项目点击?)时它崩溃了。我看不到任何触摸或点击事件处理程序。回顾一下:您修改底层适配器数据而不通知ListView关于更改。在加载数据之前调用notifyDataSetChanged也没什么意义。 – tiguchi

回答

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. 

您在non-UI thread做一个UI操作(notifyDataSetChanged()),因此,您获得的exception.Perform在主线程(UI thread)的UI操作。

试试这个:

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