Using getView().findViewById in fragment returns NullPointerException

后端 未结 1 478
无人及你
无人及你 2021-01-28 09:39

It does give me warning but I\'ve never used fragments so I\'m not sure how to fix it. I get the error on the third line of the code where I\'m trying to find the toolbar.

1条回答
  •  生来不讨喜
    2021-01-28 10:12

    You're calling getView() inside onCreateView(). By that time, the view is not set, since you're right then constructing it.

    The call to getView() will return non-null after onCreateView() and before onDestroyView().

    Instead, just call view.findViewById in the View you have just inflated, which I assume is what you want.

    LinearLayout mapsT = view.findViewById(R.id.locationT);
    

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