findViewById returns NULL when using Fragment

前端 未结 7 1888
终归单人心
终归单人心 2020-12-14 16:15

I\'m new to Android developing and of course on Fragments.

I want to access the controls of my fragment in main activity but \'findViewById\' returns null. without f

7条回答
  •  一个人的身影
    2020-12-14 17:02

    Try like this on your fragments on onCreateView

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
        if (container == null) {
            return null;
        }
    
        LinearLayout ll = (LinearLayout )inflater.inflate(R.layout.tab_frag1_layout, container, false);
    EditText txtXML = (EditText) ll.findViewById(R.id.txtXML);
    
        return ll;
    }
    

提交回复
热议问题