0

Possible Duplicate:
findViewById in fragment android

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)

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

The method findViewById(int) is undefined

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

Community
  • 1
  • 1
Boardy
  • 35,417
  • 104
  • 256
  • 447

2 Answers2

5

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.

DeeV
  • 35,865
  • 9
  • 108
  • 95
1

When you use fragments, you should override the onCreateView method.

Here is an exemple :

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        View v = inflater.inflate(R.layout.YOUR_FRAGMENT_LAYOUT, null);

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

        return v;
    }
florianmski
  • 5,603
  • 1
  • 17
  • 14