findViewById(int)方法未为类型定义。

时间:2022-10-25 20:59:44

I get the error

我得到错误的

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

findViewById(int)方法未定义为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.

CustomExpListView类基本上是复制的,从Android示例项目中粘贴过来,并且很高兴,没有问题。

Thanks you for helping me out.

谢谢你帮我。

2 个解决方案

#1


10  

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

如果R.id。ExpandableListView01是R.layout.fragment_pm_section_test的一部分,然后用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.

片段没有findViewById()方法。您需要使用您的膨胀视图,而不是从布局中获取所有内容。

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

还可以考虑使用Java命名约定。变量以小写字母开头,而类以大写字母开头。

#2


3  

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

只需通过调用getView()获取片段的根视图,然后调用findViewById()方法:

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

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

没有它,您将无法使用findViewById()作为片段类本身没有这种方法。

#1


10  

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

如果R.id。ExpandableListView01是R.layout.fragment_pm_section_test的一部分,然后用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.

片段没有findViewById()方法。您需要使用您的膨胀视图,而不是从布局中获取所有内容。

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

还可以考虑使用Java命名约定。变量以小写字母开头,而类以大写字母开头。

#2


3  

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

只需通过调用getView()获取片段的根视图,然后调用findViewById()方法:

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

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

没有它,您将无法使用findViewById()作为片段类本身没有这种方法。