ken*_*eho 7 android listview indexoutofboundsexception
我一直得到这个IndexOutOfBoundsException,但似乎无法弄清楚是什么导致它.我的listview有一个带有对象列表的适配器,并根据时间戳删除对象.删除是在getView方法内完成的.删除项后,我调用notifyDataSetChanged().
完整的源代码可以在github上找到,这里是listview适配器代码的链接:https://github.com/kenneho/run-for-the-bus/blob/master/app/src/main/java/net /kenneho/runnow/adapters/TravelsAdapter.java
这是我一直得到的堆栈跟踪的开始:
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
at android.widget.ListView.dispatchDraw(ListView.java:3307)
at android.view.View.draw(View.java:15213)
<snip>
Run Code Online (Sandbox Code Playgroud)
我看到getView中的位置值通常可以高达六或七.
这里有人可以发现这个bug吗?任何帮助将不胜感激.
此致,肯尼斯
编辑1:*链接到使用以下内容的活动代码:https://github.com/kenneho/run-for-the-bus/blob/master/app/src/main/java/net/kenneho/runnow/InfoActivity .java *我在这里粘贴了最相关的logcat部分:http://pastebin.com/5FtU4EaM
终于修复了这个bug。首先,我按照@Selvin 的建议重构了代码。其次,我添加了一个“removecallback”语句,以在创建新的 Runnable 之前停止任何 Runnable:
// Make sure we stop existing timers, if any.
timerHandler.removeCallbacks(timerRunnable);
timerRunnable = new Runnable() {
@Override
public void run() {
removeExpiredTravels(myAdapter);
myAdapter.notifyDataSetChanged();
timerHandler.postDelayed(this, 1000);
}
};
timerHandler.postDelayed(timerRunnable, 0);
Run Code Online (Sandbox Code Playgroud)
感谢您帮助我追踪这个错误。
| 归档时间: |
|
| 查看次数: |
2819 次 |
| 最近记录: |