我的 Android 列表视图适配器中的 IndexOutOfBoundsException
IndexOutOfBoundsException in my Android Listview adapter
我一直收到此 IndexOutOfBoundsException,但似乎无法弄清楚是什么原因造成的。我的列表视图有一个带有对象列表的适配器,并且根据时间戳删除对象。删除是在 getView 方法内部完成的。删除项目后,我调用 notifyDataSetChanged()。
完整的源代码可在 github 上获得,这里是列表视图适配器代码的 link: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>
我看到getView里面的position-value经常可以达到六七个。
这里有人能发现这个错误吗?任何帮助将不胜感激。
此致,
肯尼斯
编辑 1:
* Link 到 activity 使用以下代码的代码: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);
感谢您帮助我追踪此错误。
我一直收到此 IndexOutOfBoundsException,但似乎无法弄清楚是什么原因造成的。我的列表视图有一个带有对象列表的适配器,并且根据时间戳删除对象。删除是在 getView 方法内部完成的。删除项目后,我调用 notifyDataSetChanged()。
完整的源代码可在 github 上获得,这里是列表视图适配器代码的 link: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>
我看到getView里面的position-value经常可以达到六七个。
这里有人能发现这个错误吗?任何帮助将不胜感激。
此致, 肯尼斯
编辑 1: * Link 到 activity 使用以下代码的代码: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);
感谢您帮助我追踪此错误。