在ListFragment findViewById不承认 [英] findViewById not recognised in ListFragment

查看:220
本文介绍了在ListFragment findViewById不承认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
   findViewById在片段的Andr​​oid

我目前的工作在Android项目,并试图让碎片的工作。该片段部分工作,但我试图控制UI组件,在一个标准的活动,我可以使用类似

I am currently working on android project and trying to get fragments working. The fragment part is working but I'm trying to control UI components, on a standard activity I can use something like

TextView txtMyTextBox = (TextView)findViewById(R.id.my_text_box)

我通过扩展的ListFragment类,但是当我为什么尝试同样的code如上我收到以下错误

I am extending the class by ListFragment but when why I try the same code as above I get the following error

该方法findViewById(INT)未定义

The method findViewById(int) is undefined

为什么不这项工作。感谢您的帮助,您可以提供。

Why doesn't this work. Thanks for any help you can provide.

推荐答案

这方法不是在片段类这是我假设你调用它来定义。你必须使用它查看该片段被显示。这是您在 onCreateView()返回相同的观点,您可以通过检索getView()。因此, TextView的txtMyTextBox =(TextView中)getView()。findViewById(R.id.my_text_box)应该工作。

That method is not defined in the Fragment class which is where I assume you're calling it. You have to use it on the View that the Fragment is showing. This is the same view that you return in onCreateView() and you can retrieve it by getView(). Thus, TextView txtMyTextBox = (TextView)getView().findViewById(R.id.my_text_box) should work.

这篇关于在ListFragment findViewById不承认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆