在 ListFragment 中无法识别 findViewById [英] findViewById not recognised in ListFragment

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

问题描述

可能的重复:
android 片段中的findViewById

我目前正在研究 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 扩展该类,但是当我尝试与上述相同的代码时,出现以下错误

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.

推荐答案

该方法未在 Fragment 类中定义,我假设您在该类中调用它.您必须在 Fragment 显示的视图上使用它.这与您在 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天全站免登陆