Skip to content Skip to sidebar Skip to footer

Android Fragment Isn't Accepting Setcontentview

I am trying to use fragments to make swiped tabs by tablayouts (one activity with three tabs), I made three Java files for the tree tabs and the corresponding xml files as the belo

Solution 1:

You can not call setContentView in Fragment. You should override the onCreatView method of fragment and inflate your layout as follows:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View v= inflater.inflate(R.layout.tab_fragment_2, container, false);

        //to get view like textview, button etc use v.findViewById(yourid)TextViewtextView= v.findViewById(R.id.textView2);
        return v;
    }
}.

Solution 2:

For a Fragment you

  • override onCreateView
  • Inflate a view
  • use findViewById on the view reference
  • return the view

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
        Viewview= inflater.inflate(R.layout.my_fragment, container, false);
        Toolbartoolbar= (Toolbar) view.findViewById(R.id.toolbar);
        ...
        return view;
    }
    

Post a Comment for "Android Fragment Isn't Accepting Setcontentview"