Android间歇性错误onitemclick java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
Posted
技术标签:
【中文标题】Android间歇性错误onitemclick java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0【英文标题】:Android intermittent error onitemclick java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 【发布时间】:2018-04-07 08:12:23 【问题描述】:无法弄清楚这一点,我已经搜索了很多。无法在我的任何测试设备上模拟错误,但偶尔会发生在我的一些用户身上。
我有一个自定义列表视图适配器,从在线数据库中提取 6 个项目并添加到列表中。我还在列表底部添加了一个额外的静态页脚。
我知道发生此错误时列表视图中没有任何内容,它为 0,因此应用程序无法获取位置 - 由于从在线数据库中拉出字符串。
我试图通过 if/else 来解决这个问题,但仍然出现 indexoutofbounds 错误。上线:
if(mCategoryAdapter.getItem(position)!=null)
或
Category category = mCategoryAdapter.getItem(position);
我的问题是 - 我假设用户在列表完全填充之前点击了它?有什么想法可以阻止这种情况吗?
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
//assigning XML view
setContentView(R.layout.activity_category);
//fill the list view from data from parse.com using custom CategoryAdapter
mCategoryAdapter = new CategoryAdapter(this, new ArrayList<Category>());
//assigning ListView to XML ListView
mListView = (ListView)findViewById(R.id.category_list);
mListView.setAdapter(mCategoryAdapter);
customExercisesView = getLayoutInflater().inflate(R.layout.category_custom_exercises_row_item,mListView,false);
//make items in list clickable
mListView.setOnItemClickListener(this);
//parse query to retrieve the categories
getCategoryList();
public void getCategoryList()
//parse query to pull all the current available exercises from the db
ParseQuery<Category> query = ParseQuery.getQuery(Category.class).fromLocalDatastore();
//call to parse.com to start the query
query.findInBackground(new FindCallback<Category>()
@Override
public void done(List<Category> categories, ParseException error)
if(categories !=null)
//add all the categories into the list
mCategoryAdapter.addAll(categories);
//add the custom exercises footer after we have added the rest
mListView.addFooterView(customExercisesView);
//sort the list alphabetically
mCategoryAdapter.sort(new Comparator<Category>()
@Override
public int compare(Category category, Category t1)
return category.getName().compareTo(t1.getName());
);
);
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
//to make phone vibrate for 20 milliseconds when clicking an item in the list
Vibrator v = (Vibrator) this.getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(20);
//users are selecting one of the 6 main exercise categories
if(position<=5)
if(mCategoryAdapter.getItem(position)!=null)
//giving the listed exercises in the list view the ability to be clicked on
Category category = mCategoryAdapter.getItem(position);
//Get category ID and name to pass to Exercise List
mCategoryID = category.getObjectId();
mCategoryName = category.getName();
Intent exercise_intent = new Intent(this, ExerciseList.class);
exercise_intent.putExtra("categoryID", mCategoryID);
exercise_intent.putExtra("categoryName", mCategoryName);
startActivity(exercise_intent);
else
Toast.makeText(this, "Categories are still loading, please try again", Toast.LENGTH_SHORT).show();
else
//user is selecting custom exercises
Intent custom_exercise_intent = new Intent(this, CustomExerciseList.class);
startActivity(custom_exercise_intent);
异常 java.lang.IndexOutOfBoundsException:索引 0 无效, 大小为 0 java.util.ArrayList.throwIndexOutOfBoundsException (ArrayList.java:255) java.util.ArrayList.get (ArrayList.java:308) android.widget.ArrayAdapter.getItem (ArrayAdapter.java:337) adam.exercisedictionary.CategoryList.onItemClick (CategoryList.java:139) android.widget.AdapterView.performItemClick (AdapterView.java:308) android.widget.AbsListView.performItemClick (AbsListView.java:1154) android.widget.AbsListView$PerformClick.run (AbsListView.java:3074) android.widget.AbsListView$3.run (AbsListView.java:3905) android.os.Handler.handleCallback (Handler.java:739) android.os.Handler.dispatchMessage (Handler.java:95) android.os.Looper.loop (Looper.java:135) android.app.ActivityThread.main (ActivityThread.java:5595) java.lang.reflect.Method.invoke (Method.java) java.lang.reflect.Method.invoke (Method.java:372) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:960) com.android.internal.os.ZygoteInit.main (ZygoteInit.java:755)
【问题讨论】:
您正在为listview适配器设置空列表,在设置适配器之前先初始化listview项目。 你也可以发布你的适配器类 【参考方案1】:您是否检查过您的 6 个项目的数据集在该代码点是否为空或不可用?首先,检查并访问数据集中的单个项目。
我不知道它是否会起作用。试一试。 DB 可能会返回空列表。
【讨论】:
【参考方案2】:mCategoryAdapter = new CategoryAdapter(this, new ArrayList<Category>());
在这一行中,您传递了一个没有值的ArrayList
,这意味着它的大小是zero
。这里你必须通过ArrayList
已经填充了一些项目。Like:
List<Category> list = new ArrayList<Category>();
list.add(category1);
list.add(category2);
list.add(category3);
mCategoryAdapter = new CategoryAdapter(this, list);
【讨论】:
【参考方案3】:非常感谢各位,
我想通了。我确定在用户单击并启动 onclick 方法时列表视图已被填充。
我意识到我的查询是从本地数据存储中检索的,我在启动应用程序时加载了类别,同时显示了初始屏幕。问题是如果这些类别在启动时没有加载,那么这个查询将无法从本地数据存储中检索。
解决方案是通过重新尝试从 Internet 检索值来处理本地数据存储查询是否为空,并且它已经解决了问题,此后没有崩溃。
一旦用户连接到网络并下载了他们不必再次连接的类别,因为它们被永久固定在设备上。
public void getCategoryList()
//parse query to pull all the current available exercises from the db
ParseQuery<Category> query = ParseQuery.getQuery(Category.class).fromLocalDatastore();
//call to parse.com to start the query
query.findInBackground(new FindCallback<Category>()
@Override
public void done(List<Category> categories, ParseException error)
if(categories !=null)
mCategoryAdapter.clear();
//add all the categories into the list
mCategoryAdapter.addAll(categories);
else
//check we have internet
if(DetectConnection.isConnected(getApplicationContext()))
//backup get categories from net
getCategoryListFromParse();
else
//no internet and issues getting categories, let user know
Toast.makeText(getApplicationContext(),"Please check your internet connection", Toast.LENGTH_SHORT).show();
);
public void getCategoryListFromParse()
//parse query to pull all the current available exercises from the db
ParseQuery<Category> query = ParseQuery.getQuery(Category.class);
//call to parse.com to start the query
query.findInBackground(new FindCallback<Category>()
@Override
public void done(List<Category> categories, ParseException error)
if(categories !=null)
Category.pinAllInBackground(categories);
//add all the categories into the list
mCategoryAdapter.addAll(categories);
);
【讨论】:
以上是关于Android间歇性错误onitemclick java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0的主要内容,如果未能解决你的问题,请参考以下文章
Android ListView onItemClick Not Work
Android:onItemClick 和 onItemLongClick 不响应
Android:带按钮的 ListView -> OnItemClick 啥都不做
java Android Activity + onItemClicker(示例)