3

I am creating a new Tab interface and for every tab I have functionality like:

public class ClassViewFragment extends Fragment {
    ListView classListView = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.class_view, container, false);

        classListView = (ListView) findViewById(R.id.classList);

        return rootView;
    }
}

But I am getting cannot resolve method findViewById also at another place I am also unable to use runOnUiThread and getting Error:(88, 29) error: cannot find symbol method runOnUiThread(<anonymous Runnable>)

I understand these will work fine if my class was extended from Activity but it’s a tab fragment so how can I use this funcitons?

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Maven
  • 14,587
  • 42
  • 113
  • 174
  • `classListView = (ListView) rootView.findViewById(R.id.classList); – DeeV Jan 20 '15 at 18:45
  • 1
    possible duplicate of [findViewById in fragment android](http://stackoverflow.com/questions/6495898/findviewbyid-in-fragment-android) ... What can be found by simple googling... – Selvin Jan 20 '15 at 19:38
  • @Selvin yes it is. Lets vote to close it as duplicate of [findViewById in Fragment](http://stackoverflow.com/questions/6495898/findviewbyid-in-fragment)! :) – Sufian Oct 20 '16 at 07:44

7 Answers7

6

you need

classListView = (ListView) rootView.findViewById(R.id.classList);

as that is the view you are concerned with

to get runOnUiThred you need to do getActivity().runOnUiThread

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • the `rootView` object is only accesbile in `onCreateView` and not in other functions inside the class? – Maven Jan 20 '15 at 19:04
  • correct if you need it else where just hold onto it globally. You could also just hold onto the item you specifically need instead of the whole view – tyczj Jan 20 '15 at 19:05
6

You need to reference findViewById using the view,

public class ClassViewFragment extends Fragment {
    ListView classListView = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.class_view, container, false);

        classListView = (ListView) rootView .findViewById(R.id.classList);

        return rootView;
    }
}

For runOnUiThread is a possible duplicate of Modifying an Android view from a different thread

use its like,

activity.runOnUiThread(new Runnable());
Community
  • 1
  • 1
Divya Y
  • 183
  • 1
  • 9
2

Here is a shore explanation of findViewById method;

findViewById() method will work with the related object that your main layout is bound to. For example, In an activity, when you make a

setContentView(activity_main);

You imply that your Activity will retrieve its layouts from activity_main. So when you make a findViewById() in an activity, that actually means this.findViewById(), where this is your Activity.

So in your case, the layout class_view is bound to the rootView. But when you just write findViewById, it means that this.findViewById. And this is the scope you're in, which is the Fragment in your case. However, I don't think a Fragment has that capability. So you should refer to the rootView to retrieve your views.

rootView.findViewById()
Faruk Yazici
  • 2,344
  • 18
  • 38
1

Fragments don't have a findViewById() method like Activity, so you have to call the method of the root view of the fragment. Try doing rootView.findViewById(R.id.classList) instead.

emerssso
  • 2,376
  • 18
  • 24
0
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v =  inflater.inflate(R.layout.fragment_content_mediaplayer, container, false);
        mSeekbar = (SeekBar) v.findViewById(R.id.seekBarMusic);
        return v;
Pablo Cegarra
  • 20,955
  • 12
  • 92
  • 110
0

Use rootView.findViewById(R.id.classList) instead of findViewById(R.id.classList).

Try this:

public class ClassViewFragment extends Fragment {
    ListView classListView = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.class_view, container, false);
        classListView = (ListView) rootView.findViewById(R.id.classList);

        return rootView;
    }
}
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
0

findViewById must run inside onViewCreated() method

ListView classListView = null;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

    return inflater.inflate(R.layout.class_view, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    classListView = (ListView) view.findViewById(R.id.classList);
}

}`

neosonne
  • 136
  • 1
  • 4