Gridview getChildAt错误尝试在null对象上调用虚方法findViewById()

时间:2016-02-22 13:59:52

标签: android gridview adapter

我有一个gridview,其中我有24个奇数元素。

我给网格一个固定的高度,所以滚动条出现w.r.t元素没有显示。

我的问题是 下面给出的是我的gridview,其中红线表示可见网格的结尾,并且在红线下面看到必须滚动

enter image description here

说我选择6然后滚动并选择22然后再次向上滚动并选择11 我的应用程序因以下错误而崩溃。

Exception is: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.adamas.ui.AddNewBuyEntryActivity$2.onItemClick(AddNewBuyEntryActivity.java:205)
at android.widget.AdapterView.performItemClick(AdapterView.java:305)
at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
at android.widget.AbsListView$3.run(AbsListView.java:3860)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

如果我不给我的网格一个固定的高度让它包裹而不滚动我的应用程序在这种情况下不会崩溃。

发生

错误

 ((ImageView) parent.getChildAt(previousSelectedShapePosition).findViewById(R.id.img_sales_shape))
                            .setImageResource(shapeIcons.getResourceId(previousSelectedShapePosition, -1));
发生崩溃的

代码段[使用以前的位置和当前位置来切换所选元素和之前选定元素的颜色]

grdShapes.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            previousSelectedShapePosition = currentSelectedShapePosition;
            currentSelectedShapePosition = position;

            if (spnShape.getSelectedItem().toString().equalsIgnoreCase("Polished")) {
                ((ImageView) view.findViewById(R.id.img_sales_shape)).setImageResource(shapeSelectedIcons.getResourceId(position, 1));

                ((ImageView) parent.getChildAt(previousSelectedShapePosition).findViewById(R.id.img_sales_shape))
                        .setImageResource(shapeIcons.getResourceId(previousSelectedShapePosition, -1));
            } else {
                ((ImageView) view.findViewById(R.id.img_sales_shape)).setImageResource(roughShapeSelected.getResourceId(position, 1));

                ((ImageView) parent.getChildAt(previousSelectedShapePosition).findViewById(R.id.img_sales_shape))
                        .setImageResource(roughShapeIcons.getResourceId(previousSelectedShapePosition, -1));
            }
        }
    });

更新: 设置基础适配器的Spinner代码段

  spnShape.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parentView, View view, int position, long id) {
                previousSelectedShapePosition = 0;

                if (position == 0) {
                    roughORpolished = 0;
                    adapter = new GridShapeSalesAdapter(AddNewBuyEntryActivity.this, roughShapeItems,roughORpolished);
                    grdShapes.setAdapter(adapter);
                } else if (position == 1) {
                    roughORpolished = 1;
                    adapter = new GridShapeSalesAdapter(AddNewBuyEntryActivity.this, polishedShapeItems,roughORpolished);
                    grdShapes.setAdapter(adapter);
                }


            }

这是我的gridview适配器

http://pastebin.com/kaU3UFE5

http://pastebin.com/72Uq94dP

1 个答案:

答案 0 :(得分:0)

您正试图获取使用

看不到的View
 parent.getChildAt(previousSelectedShapePosition)

当孩子不可见时,你会得到null,因此NPE。

您需要将一些逻辑放到getView

<强>更新

如果您选择项目更改当前所选视图的背景,您将没有任何问题。但是,如果不可见,请不要尝试更改先前所选项目的背景。而是保留一个位置列表,当背景变得可见时需要更改背景。使用该列表作为getView中的检查并相应地更新背景。