android 中遇到的空指针异常
16lz
2021-01-23
异常信息: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);
更多相关文章
- android 动态布局setLayoutParams方法设置
- Android保持背光常亮的设置方法
- Android中定时器的3种实现方法
- Android与服务器通信的方法之一-->TCP
- Android 简单视图
- android RadioButton文字居中的方法
- 编译NotificationManagerService.java文件的方法