findViewById returns null in fragment

前端 未结 2 1560
温柔的废话
温柔的废话 2021-01-16 11:10

I have problem using the findViewByID methond in a fragment.

The spinner in Spinner spinner = (Spinner) view.findViewById(R.id.tabsearchspinner_location);

相关标签:
2条回答
  • 2021-01-16 11:35

    Inflate your View without a ViewGroup, as it's simply inside your FragmentActivity:

    View view = inflater.inflate(R.layout.fragment_tab_suggestions, null);
    

    And the way IDs are added inside the XML layout files is like this:

    android:id="@+id/tabsearchspinner_location"
    

    As you can see, you're missing the +

    0 讨论(0)
  • 2021-01-16 11:55

    Sorry, I just found out that I inflated the wrong layout, so the findViewById method always returns null, thanks for all the help though.

    0 讨论(0)
提交回复
热议问题