slider

还是菜鸟
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

findviewbyid return null for custom view

Posted on 2013-04-01 10:55  slider  阅读(607)  评论(0编辑  收藏  举报

  i invoke findViewById method like this:

view view = LayoutInflater.from(this).inflate(R.layout.xxx,null);
Customview myView = view.findviewbyid(R.id.xy);
....

setcontentView(view);

  but it keeps return null . after searchs, i found that i shoud call findviewbyid() before setcontentview method. you can also do like this:

You call this method:

inflater.inflate(R.layout.picture_method_option, null, false);
And you ignore the result. The last parameter, false, indicates to not add the content of the XML layout to the layout root (which you set to null anyway.) You need to do something like this:

View v = inflater.inflate(R.layout.picture_method_option, null, false);
addView(v);
Or:

inflater.inflate(R.layout.picture_method_option, this, true);