Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么列表小部件抛出IndexOutOfBoundsException?_Java_Android_Listview_Android Widget_Indexoutofboundsexception - Fatal编程技术网

Java 为什么列表小部件抛出IndexOutOfBoundsException?

Java 为什么列表小部件抛出IndexOutOfBoundsException?,java,android,listview,android-widget,indexoutofboundsexception,Java,Android,Listview,Android Widget,Indexoutofboundsexception,问题 我的Android应用程序有一个奇怪的崩溃报告,它有列表小部件: java.lang.IndexOutOfBoundsException:索引11无效,大小为10 位于java.util.ArrayList.ThrowindexOutfBoundsException(ArrayList.java:255) 获取(ArrayList.java:308) at me.gevorg.birth.widget.ListProvider.getViewAt(ListProvider.java:96)

问题

我的Android应用程序有一个奇怪的崩溃报告,它有列表小部件:

java.lang.IndexOutOfBoundsException:索引11无效,大小为10
位于java.util.ArrayList.ThrowindexOutfBoundsException(ArrayList.java:255)
获取(ArrayList.java:308)
at me.gevorg.birth.widget.ListProvider.getViewAt(ListProvider.java:96)
位于android.widget.RemoteViewsService$RemoteViewsFactoryAdapter.getViewAt(RemoteViewsService.java:164)
位于com.android.internal.widget.IRemoteViewsFactory$Stub.onTransact(IRemoteViewsFactory.java:85)
位于android.os.Binder.execTransact(Binder.java:453)
代码

公共类ListProvider实现RemoteViewsService.RemoteViewsFactory{
私人名单联系人名单;
私人语境;
私人意图;
私有int-appWidgetId;
私人联系人列表提供商;
公共ListProvider(上下文、意图){
this.context=上下文;
本意=本意;
//获取小部件id。
this.appWidgetId=intent.getIntExtra(
AppWidgetManager.EXTRA\u APPWIDGET\u ID,
AppWidgetManager。无效的\u APPWIDGET\u ID);
//初始化提供程序。
provider=新的ContactListProvider(上下文);
this.contactList=provider.getContacts();
}
@凌驾
public int getCount(){
返回contactList.size();
}
@凌驾
公共长getItemId(int位置){
返回contactList.get(position.getId();
}
@凌驾
公共远程视图getViewAt(内部位置){
最终RemoteViews remoteView=新的remoteView(context.getPackageName(),
R.布局、联系方式(生日);
//取得联系。
联系人=联系人列表。获取(位置);
//基于联系人生成视图。。。
返回远程视图;
}
@凌驾
public int getViewTypeCount(){
返回1;
}
@凌驾
公共布尔表ID(){
返回true;
}
@凌驾
公共无效onDataSetChanged(){
this.contactList=provider.getContacts();
}    
}
类ContactListProvider{
私人语境;
公共联系人列表提供程序(上下文){
this.context=上下文;
}
公共列表getContacts(){
列表联系人=新建ArrayList();
//在这里收集联系人。。。
返回集合。排序(联系人);
}
}
脏补丁

注意列表是只读的,由于
Contact-Contact=contactList.get(位置)而引发异常
if(位置>=contactList.size()){
返回null;
}
//这一行导致异常!!!
联系人=联系人列表。获取(位置);
问题


显然,这并不是在解决问题,而是在隐藏它,我需要您的帮助来理解是什么导致了这个
索引自动边界异常

您是否覆盖了getCount?它是否返回联系人列表的大小?是的,我也添加了它。这非常痛苦-然后-尝试边缘案例,删除所有联系人,例如,尝试自己破坏它。你找到了吗?面临同样的问题。@korro不,我现在使用的是脏补丁。您是否覆盖getCount?它是否返回联系人列表的大小?是的,我也添加了它。这非常痛苦-然后-尝试边缘案例,删除所有联系人,例如,尝试自己破坏它。你找到了吗?面对同样的问题。@korro不,肮脏的修复是我现在使用的