The findViewById (int) method has an undefined value for the type

I get an error

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

when trying to implement my extensible ListView. I'm sure this is a common mistake, but it's hard for me to find a solution. I'm trying to learn fragments, etc., and since then I started a hard battle. I worked a little on the search and found a lot of results that don't seem to help me in my case.

If someone can give me any idea or point me in the right direction, I would really appreciate it.

My little test class 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 a copy inserted from Android Model Projects, and is happy and has no problems.

Thanks for helping me.

+6
source share
2 answers

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

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

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

Also consider using Java naming conventions. Variables begin with lowercase letters, while Classes begin with uppercase letters.

+10
source

Just find the root view of the fragment by calling getView() , and then call the findViewById() method:

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

Without it, you cannot use findViewById() , since the Fragment class itself does not have such a method.

+3
source

All Articles