findViewById 为空的可能原因
2012-03-05 19:52
281 查看
三种可能原因:
1) 在setContentView之前调用了,解决方法: 在setContentView之后调用。
2) findViewById所属的VIew如果是inflater出来的,那么需要加上inflaterView.findViewById(R.id.xx)
3) 很郁闷的一种错,常发生在inflater之后调用,View不空,但是find之后抛空,其实是Eclipse本身的原因,解决方法: Clean Project-Refresh-Build, 问题解决~~~
1) 在setContentView之前调用了,解决方法: 在setContentView之后调用。
@Override public void onCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); ListView lv = (ListView)findViewById(R.id.tx); setContentView(R.layout.main); //… }
2) findViewById所属的VIew如果是inflater出来的,那么需要加上inflaterView.findViewById(R.id.xx)
ListView lv = (ListView)inflaterView.findViewById(R.id.xx)
3) 很郁闷的一种错,常发生在inflater之后调用,View不空,但是find之后抛空,其实是Eclipse本身的原因,解决方法: Clean Project-Refresh-Build, 问题解决~~~
View rowview = (View)inflater.inflate(R.layout.rowview, parent, false); TextView tx =(TextView)rowview.findViewById(R.id.tx); TextView tc =(TextView)rowview.findViewById(R.id.tc);
相关文章推荐
- findViewById返回null的原因
- android中findViewById(R.id.XXX)提示空指针异常NullPointerException的原因
- Android findViewById()找不到自定义View对象的一种原因
- android开发中布局和代码块不能同步(可能findViewById找不到对应布局的id),这是因为安装的android stodio没有设置自动!
- 【总结】在两个Activity之间进行跳转时出错原因分析(一)——findViewById位置不当导致
- 【ANDROID】Android 在oncreate() 方法中调用 findViewById 返回 null 问题的原因和解决方法
- android初学笔记之<findViewById方法返回Null的原因>
- findViewById需要进行类型转换的原因
- 懒人的findviewbyid
- view.findViewById 和Activity.findViewById区别
- findViewById is not public
- [转]Jollen 的 Android 教學,#6: WebView 體驗與 findViewByID
- 根据XML文件的id自动写java文件的声明方法和findViewById方法
- Gallery g = (Gallery)findViewById(R.id.secondButton);
- kotlin Android中 findViewById()不能推导类型的错误
- Jollen 的 Android 教学:WebView 体验与 findViewByID
- Ioc依赖注入,使你的项目告别findViewById
- inflate()和findViewById()函数的用法
- 在 fragment 里面调用 findViewById
- findViewById()返回null