【发布时间】: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项目。
-
你也可以发布你的适配器类
标签: android listview arraylist indexoutofboundsexception onitemclick