您的位置:首页 > 其它

findViewById 为空的可能原因

2012-03-05 19:52 281 查看
三种可能原因:

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