AndroidDev 板


LINE

各位前輩大家好,小弟接觸android沒多久還是位新手, 目前在做作品時遇到了一個瓶頸,是在ListView.setAdapter() 短時間內大量重複更新時跳出以下錯誤: 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. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131296313, class android.widget.ListView) with Adapter(class com.example.avalon.PlayerAdapter)] 以下是小弟寫的Adapter的程式碼: public class PlayerAdapter extends BaseAdapter { private LayoutInflater playersInflater; public List<Player> playerslist; public PlayerAdapter(Context context, List<Player> playerslist) { // TODO 自動產生的建構子 Stub if(playerslist!=null){ playersInflater=LayoutInflater.from(context); this.playerslist=playerslist; PlayerAdapter.this.notifyDataSetChanged(); } } @Override public int getCount() { // TODO 自動產生的方法 Stub return (playerslist==null)? 0 : playerslist.size(); } @Oerride public Object getItem(int position) { // TODO 自動產生的方法 Stub return playerslist.get(position); } @Override public long getItemId(int position) { // TODO 自動產生的方法 Stub return playerslist.indexOf(getItem(position)); } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO 自動產生的方法 Stub if(playerslist.size()!=0){ try{ convertView=playersInflater.inflate (R.layout.players_list_item, null); ImageView iv_Players_Status=(ImageView) convertView.findViewById(R.id.IV_Players_Status); TextView tv_Players_Order=(TextView) convertView.findViewById(R.id.TV_Players_Order); TextView tv_Players_Anonymous=(TextView) convertView.findViewById(R.id.TV_Players_Anonymous); iv_Players_Status.setImageResource (playerslist.get(position).getStatus()); tv_Players_Order.setText(String.valueOf (playerslist.get(position).getOrder()).toString()); tv_Players_Anonymous.setText (playerslist.get(position).getAnonymity()); }catch(Exception e){ e.printStackTrace(); Log.e("text","PlayerAdapter="+e.toString()); } return convertView; }else{ convertView=playersInflater.inflate(R.layout.null_layout, null); return convertView; } } } 這幾天爬文有看到getCount()內寫成 return (playerslist==null)? 0 : playerslist.size(); 還有在更新資料時呼叫notifyDataSetChanged()通知系統更新Adapter 可是在大量更新時app還是會崩潰跳出此錯誤 有聽人說可以在更新時隱藏按鈕,到更新完後在顯示按鈕 但是小弟是寫socket連伺服器,所以可以多人更新所以以上方法可能無法解決 還希望各位可以協助解決,謝謝~ --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.175.134.228
※ 文章網址: https://webptt.com/m.aspx?n=bbs/AndroidDev/M.1444146266.A.E50.html
1F:推 chaos88218: Socket有沒有開另外一個Thread,有的話Socket跟UI資訊 10/07 01:20
2F:→ chaos88218: 更新也很容易造成兩個Thread在協同資料上有困難,不 10/07 01:20
3F:→ chaos88218: 知道是不是這個問題 10/07 01:20
4F:→ chaos88218: 只要有thread影響到Ui執行緒就會跳出錯誤,handler在 10/07 01:28
5F:→ chaos88218: 大量資訊處理也會因為資料順序更新問題使得ui執行緒 10/07 01:28
6F:→ chaos88218: 收到指令時出問題 10/07 01:28
7F:→ x049: 我有用一個Thread在背景做接收 10/07 12:37
8F:→ x049: 接收完後再用runOnUiThread()做ListView.setAdapter() 10/07 12:38
9F:→ x049: 之前有過出現null值的錯誤,所以我在接收時有用ArrayList<> 10/07 12:41
10F:→ x049: 儲存背景接收到的資料再作setAdapter()才不被下一個接收覆蓋 10/07 12:43
11F:→ x049: 之前因為這個問題在爬文時有看人說過 10/07 12:47
12F:→ x049: 可能是在大量處理時adapter會改由Thread執行,而非UI 10/07 12:48
13F:→ x049: 在呼叫notifyDataSetChanged()時才會產生該錯誤 10/07 12:48
14F:→ x049: 只是不知道該如何解決 10/07 13:03
15F:→ ssccg: 你背景thread和UI thread在動同一個adapter instance? 10/07 18:36
16F:→ ssccg: 應該背景接到list後,UI thread把那個list內容copy到 10/07 18:37
17F:→ ssccg: adapter裡的list,再notifyDataSetChanged就好吧 10/07 18:37
18F:→ x049: 我背景只有做接收到ArrayList<> 10/07 22:04
19F:→ x049: 而後來的存取ArrayList<>與adapter裡的list,再到 10/07 22:05
20F:→ x049: 都是使用runOnUiThread()完成,而notifyDataSetChanged是放在 10/07 22:06
21F:→ x049: adapter內,如文中程式這樣,我不確定是不是有用錯 10/07 22:06
22F:→ x049: 目前只有在大量處理時會跳錯誤,我想說最多10個人同時點擊 10/07 22:10
23F:→ x049: 所以為了測試,在伺服器寫了點擊一次送10次更改 10/07 22:11
24F:→ x049: 在瘋狂點擊幾秒後才會出現這個錯誤 10/07 22:11
25F:→ corrupt003: getItemId() 可直接回傳position 10/07 23:01
26F:→ corrupt003: 你怎麼把更新過的資料copy到adapter的? 10/07 23:02
27F:→ corrupt003: 要記得arraylist裡的東西都是物件,直接用 = 是指到同 10/07 23:04
28F:→ corrupt003: 一個reference 10/07 23:04
29F:→ corrupt003: this.playerslist=playerslist; 你建構子中的這行就 10/07 23:06
30F:→ corrupt003: 很可能出問題 10/07 23:06
31F:→ corrupt003: 你也可以改用或參考ArrayAdapter的程式碼,它是 threa 10/07 23:16
32F:→ corrupt003: d-safe 10/07 23:16
33F:→ x049: 感謝大大,改成new一個List<Player>再用addAll()複製過去 10/07 23:49
34F:→ x049: 解決這個錯誤了,只是我在remove時貌似出現新的錯誤 10/07 23:51
35F:→ x049: java.util.ConcurrentModificationException 10/07 23:51
36F:→ corrupt003: 有用Iterator嗎?這個錯誤google一下有不少人討論 10/07 23:54
37F:→ x049: 抱歉剛剛以解決,原來是我用Log勘查ArrayList<> 10/07 23:57
38F:→ x049: 每次存取都查看ArrayList<>累積多少,可能是有些被Thread刪除 10/07 23:58
39F:→ x049: 才會跳出這個錯誤 10/07 23:58







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:iOS站內搜尋

TOP