2013-08-26 72 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(); 
      }});