作者x049 (joker)
看板AndroidDev
标题[问题] java.lang.IllegalStateException
时间Tue Oct 6 23:44:19 2015
各位前辈大家好,小弟接触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/cn.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