liangjifanzhuan2
斜月三星洞
采纳率50%
2016-03-21 12:58 阅读 1.8k

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 103style 2016-03-22 01:33

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

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

    点赞 评论 复制链接分享
  • qq_17250009 一口仨馍 2016-03-21 13:26

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

    点赞 评论 复制链接分享
  • qq_27578023 qq_27578023 2016-06-04 08:27

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

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

    点赞 评论 复制链接分享

相关推荐