android 中遇到的空指针异常

异常信息:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference


1、homePageTab = (LinearLayout) findViewById(R.layout.home_page_tab);
2、goodsGridView = (GridView) view01.findViewById(R.id.gridView);
3、LayoutInflater inflater = LayoutInflater.from(this);
        View view01 = inflater.inflate(R.layout.home_page_tab, null);

在代码中执行到第二步报错,出现空指针异常,打日志发现homePageTab是空的,第一步并不是真正的得到这个控件对象,只有得到这个对象了才可以调用它的方法。

第三步就是把布局文件转化成对应的视图对象,这样就可以调用其方法了。

正确步骤为:

LayoutInflater inflater = LayoutInflater.from(this);
        View view01 = inflater.inflate(R.layout.home_page_tab, null);
goodsGridView = (GridView) view01.findViewById(R.id.gridView);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值