2 liangjifanzhuan2 liangjifanzhuan2 于 2016.03.21 20:58 提问

android 中自定义GridView的使用

楼主最近在学习安卓,用和ListView类似的方法实现Gridview的时候出现了问题。希望高手能指点一二。

aty_using_gridview.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/gridView"
        android:numColumns="3"/>
</LinearLayout>

grid_view_cell.xml

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/img_grid_view" />
</FrameLayout>

这个是Activity

 public class AtyUsingGridView extends Activity {


    private GridView  gridView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_using_gridview);


        ImageGridViewAdapter adapter=new ImageGridViewAdapter(this);
        gridView.findViewById(R.id.gridView);
        gridView.setAdapter(adapter);
    }
}


这个是adapter

 public class ImageGridViewAdapter extends BaseAdapter {
        private Context context=null;
        private  ImageGridViewData data[]= {
                new ImageGridViewData( R.drawable.img2),
                new ImageGridViewData(R.drawable.img2),
                new ImageGridViewData( R.drawable.img2),
        };

        public ImageGridViewAdapter(Context context){
            this.context=context;
        }
        public Context getContext() {
            return context;
        }

        @Override
        public int getCount() {
            return data.length;
        }

        @Override
        public ImageGridViewData getItem(int i) {
            return data[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            FrameLayout fl=null;
            if(view!=null){
                fl=( FrameLayout)view;
            }else{
                fl= ( FrameLayout) LayoutInflater.from(getContext()).inflate(R.layout.grid_view_cell,null);
            }
            ImageGridViewData data=getItem(i);
            ImageView imgID=(ImageView)fl.findViewById(R.id.img_grid_view);

            imgID.setImageResource(data.imgID);

            return fl;
        }
    }

运行出现错误如下
Unable to start activity
java.lang.NullPointerException

我这有什么错误,希望高手给指出啊,万分感谢。

3个回答

lxk_1993
lxk_1993   Rxr 2016.03.22 09:33
已采纳

gridView.findViewById(R.id.gridView); 这里你没有实例化gridview

改为gridview = gridView.findViewById(R.id.gridView)

qq_17250009
qq_17250009   2016.03.21 21:26

gridView.findViewById(R.id.gridView);
改成 gridView = (GridView)findViewById(R.id.gridView);

qq_27578023
qq_27578023   2016.06.04 16:27

gridView.findViewById(R.id.gridView); 这里你没有实例化gridview

改为gridview = gridView.findViewById(R.id.gridView)

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!