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.
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);
Fragment
s 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With