user971741
user971741

Reputation:

Using findViewById inside a Fragment

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?

Upvotes: 3

Views: 12750

Answers (7)

Soulless Student
Soulless Student

Reputation: 196

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);
}

}`

Upvotes: 0

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21736

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;
    }
}

Upvotes: 0

Pablo C. Garc&#237;a
Pablo C. Garc&#237;a

Reputation: 22394

 @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;

Upvotes: 0

Faruk Yazici
Faruk Yazici

Reputation: 2404

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()

Upvotes: 1

Divya Y
Divya Y

Reputation: 173

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());

Upvotes: 5

emerssso
emerssso

Reputation: 2386

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.

Upvotes: 0

tyczj
tyczj

Reputation: 73721

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

Upvotes: 5

Related Questions