android - 该类型未定义方法 findViewById(int)

标签 android fragment expandablelistview

我得到了错误

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.ExpandableListView01R.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/

相关文章:

android - 如何为没有 child 的组隐藏 ExpandableListView 指示器

android - 将选中的项目 move 到可展开的 ListView 中

android 系统尝试绘制大尺寸位图(崩溃报告)

android - OnItemClickListener ListView

android - 屏幕解锁时崩溃

java - IllegalStateException 在 Activity 中替换 fragment 时找不到 fragment 的 View

java - 在多个 fragment 中显示日期

java - 为什么我的程序在启动时崩溃?

android - 按钮在 Fragment 内部单击以打开新的 Fragment

java - ExpandableListView OnChildClickListener 不起作用