findViewById not recognised in ListFragment [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-01 13:51:49

That method is not defined in the Fragment class which is where I assume you're calling it. You have to use it on the View that the Fragment is showing. This is the same view that you return in onCreateView() and you can retrieve it by getView(). Thus, TextView txtMyTextBox = (TextView)getView().findViewById(R.id.my_text_box) should work.

When you use fragments, you should override the onCreateView method.

Here is an exemple :

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        View v = inflater.inflate(R.layout.YOUR_FRAGMENT_LAYOUT, null);

        TextView txtMyTextBox = (TextView)v.findViewById(R.id.my_text_box)

        return v;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!