今天在重写BaseAdapter的getView()方法时,下面两行代码报空指针。
TextView tv_title = (TextView) findViewById(R.id.tv_title);
ImageView iv_icon = (ImageView) findViewById(R.id.iv_icon);
后来查了一下,findViewById()返回null的话,有以下原因:
1.没有载入控件所在布局
这个不是我所产生的问题,在前面加上
View view = LayoutInflater.from(HomeActicity.this).inflate(R.layout.gridview_item,null);就可以
2.findViewById的完整写法是View.findViewById(),而不指定View时默认的是Context,因此当findViewById不是在context里执行时,要指定对应的View。
我就是这个问题,在findViewById()前面加上view就可以