当用户 holder class 在数组适配器中保存视图时程序崩溃

Program crash when user holder class to hold view in array adapter

我想实现一个网格视图,显示设备库中的照片列表。在我将 gridview 的第一个位置添加到我自己的自定义数据(将用户带到照片意图的图像)而不是照片数据之前,它工作正常。当我尝试从持有者 class 访问视图元素时崩溃(例如更改 imageView 的背景颜色)。我在我的代码中出现问题的地方注释掉它。

getView 方法:

public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        Holder holder = null;
        if(position == 0) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.grid_view, parent, false);
            row.findViewById(R.id.imageView).setBackgroundColor(getResources().getColor(R.color.red));
        }else{
            if(row == null){
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.grid_view, parent, false);
                holder = new Holder(row);
                row.setTag(holder);
            }else{
                holder =(Holder) row.getTag();

            }

            //here is the problem why program crash
            holder.image.setBackgroundColor(getResource().getColor(R.id.green));



        }

        return row;

}

持有人类

class Holder{
    ImageView image;
    int position;
    Holder(View v){

        image = (ImageView) v.findViewById(R.id.imageView);

    }

}

grid_view.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

    <ImageView android:id="@+id/imageView" 
        android:layout_width="110dp"
        android:layout_height="110dp"
        android:layout_centerInParent="true"
        android:background="#82c8fff8"
        android:padding="2dp"
        android:src="@mipmap/ic_launcher" />

</RelativeLayout>

LogCat错误

03-23 17:20:24.181    6615-6615/com.example.khooteckwei.baseadapter E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.example.khooteckwei.baseadapter.Photo$ImageAdapter.getView(Photo.java:193)
        at android.widget.AbsListView.obtainView(AbsListView.java:2456)
        at android.widget.GridView.makeAndAddView(GridView.java:1331)
        at android.widget.GridView.makeRow(GridView.java:331)
        at android.widget.GridView.fillDown(GridView.java:283)
        at android.widget.GridView.fillFromTop(GridView.java:407)
        at android.widget.GridView.layoutChildren(GridView.java:1219)
        at android.widget.AbsListView.onLayout(AbsListView.java:2307)
        at android.view.View.layout(View.java:14107)
        at android.view.ViewGroup.layout(ViewGroup.java:4657)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
        at android.view.View.layout(View.java:14107)
        at android.view.ViewGroup.layout(ViewGroup.java:4657)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14107)
        at android.view.ViewGroup.layout(ViewGroup.java:4657)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
        at android.view.View.layout(View.java:14107)
        at android.view.ViewGroup.layout(ViewGroup.java:4657)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14107)
        at android.view.ViewGroup.layout(ViewGroup.java:4657)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
        at android.view.View.layout(View.java:14107)
        at android.view.ViewGroup.layout(ViewGroup.java:4657)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:14107)
        at android.view.ViewGroup.layout(ViewGroup.java:4657)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2005)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1826)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1121)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4598)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
        at android.view.Choreographer.doCallbacks(Choreographer.java:555)
        at android.view.Choreographer.doFrame(Choreographer.java:525)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4921)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
        at dalvik.system.NativeStart.main(Native Method)

你可以这样写颜色代码试试..

e.g. holder.image.setBackgroundColor(Color.parseColor("#000000"));

首先你的xml是错误的。不存在 android:tag 字段,而您正在尝试获取和设置标签,然后它会崩溃。

重写你的XML:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

    <ImageView android:id="@+id/imageView" 
        android:layout_width="110dp"
        android:layout_height="110dp"
        android:layout_centerInParent="true"
        android:background="#82c8fff8"
        android:padding="2dp"
        android:src="@mipmap/ic_launcher"
android:tag="0" />

</RelativeLayout> 

而且代码getView()方法也没有写好。重写它。参考以下。

希望能解决。

public View getView(int position, View convertView, ViewGroup parent) {
                View row = convertView;
                Holder holder = null;
                if(row == null){
                    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    row = inflater.inflate(R.layout.grid_view, parent, false);
                    holder = new Holder();
                    holder.image = (ImageView)row.findViewById(R.id.imageview);
                    row.setTag(holder);
                }else{
                    holder =(Holder) row.getTag();

                }

    // set your data here.
    if(position == 0){
    holder.image.setBackgroundColor(context.getResource.getColor(R.color.red))
    }
    else{
    holder.image.setBackgroundColor(context.getResource.getColor(R.color.green))
    }
        return row;
        }

class Holder
{
    ImageView image;
}