Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The method findViewById(int) is undefined for the type

I get the error

The method findViewById(int) is undefined for the type PM_section

when trying to implement my Expandable ListView. I am sure this is a common error, but I am having trouble finding a solution. I am attempting to learn fragments and such, and it has been an uphill battle ever since I started. I have done a fair bit of searching, and found a lot of results, that don't seem to help me in my case.

If someone could give me any insight, or point me in the right direction I would greatly appreciate it.

My small test class is below

public class PM_section extends Fragment {
//  CustomExpListView custExpListView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        View V = inflater.inflate(R.layout.fragment_pm_section_test, container, false);

        CustomExpListView custExpListView = (CustomExpListView)
                this.findViewById(R.id.ExpandableListView01);
        return V;   
    }// end on create
}

The CustomExpListView class was pretty much copy, pasted from the Android Sample Projects, and is happy and has no problems.

Thanks you for helping me out.

like image 238
gbrewster Avatar asked Jan 21 '13 22:01

gbrewster


Video Answer


2 Answers

If R.id.ExpandableListView01 is part of R.layout.fragment_pm_section_test, then replace this with V.

CustomExpListView custExpListView = (CustomExpListView)
                V.findViewById(R.id.ExpandableListView01);

Fragments do not have a findViewById() method. You need to use your inflated View instead to get everything from your layout.

Also consider using Java naming conventions. Variables start with lowercase letters while Classes start with Uppercase letters.

like image 144
A--C Avatar answered Nov 15 '22 10:11

A--C


Simply get the fragment's root view by calling getView() and then call findViewById() method:

getView().findViewById(R.id.ExpandableListView01);

Without it you will not be able to use findViewById() as Fragment class doesn't itself have such method.

like image 45
Piotr Chojnacki Avatar answered Nov 15 '22 10:11

Piotr Chojnacki