我得到了错误
The method findViewById(int) is undefined for the type PM_section
在尝试实现我的可扩展 ListView 时。我确信这是一个常见错误,但我无法找到解决方案。我正在尝试学习 fragment 等,自从我开始以来,这一直是一场艰苦的战斗。我进行了相当多的搜索,发现了很多结果,但这些结果似乎对我的情况没有帮助。
如果有人能给我任何见解,或者指出正确的方向,我将不胜感激。
下面是我的小测试类
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
}
CustomExpListView 类几乎是从 Android 示例项目中复制和粘贴的,很高兴并且没有问题。
谢谢你帮助我。
最佳答案
如果 R.id.ExpandableListView01
是 R.layout.fragment_pm_section_test
的一部分,则将 this
替换为 V
.
CustomExpListView custExpListView = (CustomExpListView)
V.findViewById(R.id.ExpandableListView01);
Fragment
没有 findViewById()
方法。您需要使用膨胀的 View
来获取布局中的所有内容。
同时考虑使用 Java 命名约定。变量以小写字母开头,而类以大写字母开头。
关于android - 该类型未定义方法 findViewById(int),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14448448/