您的位置:首页 > 其它

findViewById()返回null

2012-04-11 23:03 405 查看
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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: