Following this guide I'm trying to setup a fragment but I have an error here:

getSupportFragmentManager().beginTransaction()
    .add(R.id.fragment_container, myfrag).commit();

Can't resolve method add(int, com.mypackage.MyFragment)

I have a FrameLayout with a fragment_container id and MyFragment Extends Fragment... So I'm not sure what I'm doing wrong here... First time using fragments...

Here is my code

Main Activity:

public class MainFragmentActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_fragmentactivity);
        int frag = getIntent().getExtras().getInt("fragment");

        if(findViewById(R.id.fragment_container) != null) {
            if(savedInstanceState != null) return;

            MyFragment myfrag= new MyFragment();
                        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, myfrag).commit();

...

MyFragment:

public class MyFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.settings_devicelist, container, false);
    }

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);

        listView = (ListView)getView().findViewById(R.id.lvHosts);
...

settings_framentactivity.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Related posts

Recent Viewed