如何解决Android中的java.lang.IndexOutOfBoundsException?
问题描述:
我创建了一个可展开的列表视图,在单击子元素时启动了另一个活动。但是这仅发生在第一个父组中的前三个子元素,当我点击后续子元素时,该应用崩溃了一个错误如何解决Android中的java.lang.IndexOutOfBoundsException?
java.lang.IndexOutOfBounds:无效指数3,大小为3
我不知道我在哪里出了问题。
这是MainActivity.java
文件
package com.example.expandablelistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View view,
int groupPosition, int childPosition, long id) {
// selected item
if(listDataHeader.get(groupPosition)=="Catalog"){
Intent i = new Intent(MainActivity.this, SingleListItem.class);
// sending data to new activity
startActivity(i);
}
if(listDataHeader.get(groupPosition)=="My Account"){
Intent i1 = new Intent(MainActivity.this,CheckHolds.class);
startActivity(i1);
}
if(listDataHeader.get(groupPosition)=="Library Info"){
Intent i2 = new Intent(MainActivity.this,LibraryHours.class);
startActivity(i2);
}
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataHeader.add("Catalog");
listDataHeader.add("My Account");
listDataHeader.add("Library Info");
// Adding child data
List<String> catalog = new ArrayList<String>();
catalog.add("Automobile");
catalog.add("Civil");
catalog.add("Electronics and Communication");
catalog.add("Electrical and Electronics");
catalog.add("Information Science");
catalog.add("Industrial Production");
catalog.add("Mechanical");
catalog.add("Basic Sciences");
List<String> myaccount = new ArrayList<String>();
myaccount.add("Check Holds");
myaccount.add("Unreserve Books");
List<String> libraryinfo = new ArrayList<String>();
libraryinfo.add("Library Hours");
libraryinfo.add("Contact Library");
listDataChild.put(listDataHeader.get(0), catalog); // Header, Child data
listDataChild.put(listDataHeader.get(1), myaccount);
listDataChild.put(listDataHeader.get(2), libraryinfo);
}
}
这是ExpandableListAdapter.java文件
package com.example.expandablelistview;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
请给我建议,我应该做些什么改变来解决错误。
答
尝试访问该组元素如下:
listDataHeader.get(groupPosition).equalsIgnoreCase("My Account")
还访问子元素你可以得到它如下:
listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition).equalsIgnoreCase("Automobile");
更改您的孩子单击以下收听者:
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View view,
int groupPosition, int childPosition, long id) {
// selected item
if(listDataHeader.get(groupPosition).equalsIgnoreCase("Catalog")){
Intent i = new Intent(MainActivity.this, SingleListItem.class);
// sending data to new activity
startActivity(i);
}
if(listDataHeader.get(groupPosition).equalsIgnoreCase("My Account"){
Intent i1 = new Intent(MainActivity.this,CheckHolds.class);
startActivity(i1);
}
if(listDataHeader.get(groupPosition).equalsIgnoreCase("Library Info"){
Intent i2 = new Intent(MainActivity.this,LibraryHours.class);
startActivity(i2);
}
return false;
}
答
让我们考虑这一点,如果条件:
if(listDataHeader.get(groupPosition)=="My Account"){
这里你应该先检查是否groupPosition
是列表listDataHeader
的大小,以避免这种Exception
内。例如groupPosition < listDataHeader.size()
此处您还在以错误的方式进行字符串比较。 Java不支持这种比较。您应该使用它来代替==
要么.equals("My Account")
或.equalsIgnoreCase("My Account")
所以最后这将是:
if(groupPosition < listDataHeader.size() &&
listDataHeader.get(groupPosition).equalsIgnoreCase("My Account")){
post logcat .... –
您在哪条线上得到异常? –
一个没有注意到的bug:'listDataHeader.get(groupPosition)==“我的账户”错误。使用'equals()'进行字符串值比较。 'listDataHeader.get(groupPosition).equals(“我的账户”)'。 – SudoRahul