簡體   English   中英

方法findViewById(int)未定義類型

[英]The method findViewById(int) is undefined for the type

我收到了錯誤

對於PM_section類型,方法findViewById(int)未定義

在嘗試實現我的可擴展ListView時。 我確信這是一個常見的錯誤,但我找不到解決方案。 我試圖學習碎片等等,自從我開始以來,它一直是一場艱苦的戰斗。 我做了很多搜索,發現了很多結果,在我的情況下似乎沒有幫助我。

如果有人能給我任何見解,或指出我正確的方向,我將非常感激。

我的小測試課程如下

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 ,然后更換thisV

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

Fragment沒有findViewById()方法。 您需要使用膨脹的View來獲取布局中的所有內容。

還要考慮使用Java命名約定。 變量以小寫字母開頭,而類以大寫字母開頭。

只需通過調用getView()然后調用findViewById()方法獲取片段的根視圖:

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

沒有它你將無法使用findViewById()因為Fragment類本身沒有這樣的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM