如何解决android中的java.lang.IndexOutOfBoundsException?

时间:2014-03-07 08:48:55

标签: java android

我创建了一个可扩展的列表视图,在单击子元素时,另一个活动已启动。但这只发生在第一个父组中的前三个子元素,当我点击后续子元素时,应用程序崩溃了一个错误

  

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;
     }
    }

请建议我应该采取哪些措施来解决错误。

2 个答案:

答案 0 :(得分:1)

让我们考虑一下条件:

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")){

答案 1 :(得分:0)

尝试访问Group元素,如下所示:

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;
    }