Gridview ArrayIndexOutOfBoundsException崩溃

时间:2017-11-08 21:39:09

标签: android gridview indexoutofboundsexception

我没有看到我的gridview有任何问题,但是当我跑步时,它会崩溃 在gradle构建中没有任何错误。 这是我的gridview 主:

SELECT * FROM (...existing query...) as foo
WHERE TagName LIKE '%Tag 1%'

适配器:

GridView grid;
public  boolean blablabla = true;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.first_frag, container, false);

    ArrayList<String> web = new ArrayList<String>();
    ArrayList<Integer> imageId = new ArrayList<Integer>();




    web.add("test");
    web.add("test");
    web.add("test");

    for (int i = 0; i < web.size(); i++) {
        switch (i) {
            case 0:
                if (blablabla== true) {
                    imageId.add(R.drawable.ic_luncher);
                } else {
                    imageId.add(R.drawable.ic_luncher);
                }
                break;
            case 1:
                if (lvl1 == true) {
                    //set image 1
                } else {
                    //set image 2
                }
                break;
            case 2:
                if (lvl2 == true) {
                    //set image 1
                } else {
                    //set image 2
                }
                break;
            default:

        }

    }





    CustomGrid adapter = new CustomGrid(getActivity(), web.toArray(new String[web.size()]), imageId.toArray(new Integer[imageId.size()]));
    grid=(GridView) v.findViewById(R.id.grid);
    grid.setAdapter(adapter);

logcat的:

  

致命的例外:主要                                                                             java.lang.ArrayIndexOutOfBoundsException:length = 1;索引= 1                                                                                 at example.examplee.com.newproject.CustomGrid.getView(CustomGrid.java:58)                                                                                 在android.widget.AbsListView.obtainView(AbsListView.java:2207)                                                                                 在android.widget.GridView.makeAndAddView(GridView.java:1341)                                                                                 在android.widget.GridView.makeRow(GridView.java:341)                                                                                 在android.widget.GridView.fillDown(GridView.java:283)                                                                                 在android.widget.GridView.fillFromTop(GridView.java:417)                                                                                 在android.widget.GridView.layoutChildren(GridView.java:1229)

由于

1 个答案:

答案 0 :(得分:0)

1. a[i]的长度为web。因此3的长度应为Imageid

2.如果3的长度为Imageid,则会导致代码中出现1

请检查ArrayIndexOutOfBoundsException长度,web长度应相同

你可以添加所有图片吗?

Imageid