findViewById()返回null

来源:互联网 发布:excel的数据库功能 编辑:程序博客网 时间:2024/05/02 01:17
                Log.i("===","q");Log.i("===",""+R.id.mainlayout);Log.i("===",""+findViewById(R.id.mainlayout));final PopupWindow popupWindow = new PopupWindow(        view1.findViewById(R.id.mainlayout), 200, 300);

做手势识别,弹出popwindow,不过加载自定义的layout的时候,总是报findviewbyid()nullpointer,用log测试的时候,说它返回的是null。。无语,找了好久,想了好久,总是不对。。一晚上过去了实在没有办法了,疯狂百度,google。。网上说的原因大概有三点:

1:eclipse的原因,只需要clean一下即可。

2:findviewbyid在setcontentview之前,没有找到。解决方案是吧findviewbyid放在后面。

3:findviewbyid是在当前的context对应的layout下面找id,跨xml或者activitity的话就会返回null,所以要在自己对应的xml里面去findview

我的是在一个自定义的view里面找的id,要寻找main.xml里面的id,所以总要报错。

View view1=View.inflate(con,R.layout.main,null);  final PopupWindow popupWindow = new PopupWindow(view1.findViewById(R.id.mainlayout), 200, 300);


原创粉丝点击