无法想出这个,我搜索了很多。无法模拟我的任何测试设备上的错误,但偶尔会发生在我的某些用户身上。
我有一个自定义列表视图适配器,从在线数据库中提取了6个项目并添加到列表中。我还在列表的底部添加了一个额外的静态页脚。
据我所知,当发生此错误时,listview中没有任何内容,它为0,因此app无法获取位置 - 因为从在线数据库中拉出字符串。
我试图通过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无效, size is 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)
答案 0 :(得分:0)
您是否检查过6个项目的数据集是空的还是在该代码点不可用?首先,检查并访问数据集中的单个项目。
我不知道它是否会起作用。试试看。 DB可能会返回空列表。
答案 1 :(得分:0)
mCategoryAdapter = new CategoryAdapter(this, new ArrayList<Category>());
在此行中,您传递的ArrayList
没有值,表示其大小为zero
。在这里你必须通过已经填充了一些项目的ArrayList
。赞:
List<Category> list = new ArrayList<Category>();
list.add(category1);
list.add(category2);
list.add(category3);
mCategoryAdapter = new CategoryAdapter(this, list);
答案 2 :(得分:0)
非常感谢你们,
我明白了。我确定在用户点击并启动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);
}
}
});
}