Подтвердить что ты не робот

Попытка добавить фрагмент в мой контейнер-фрагмент FrameLayout

Я создал xml файл с именем editor.xml, который содержит FrameLayout. В моей основной деятельности я пытаюсь добавить свой собственный фрагмент в свой FrameLayout.

Ошибка при попытке добавить мой фрагмент:

Метод add (int, Fragment) в типе FragmentTransaction не применим для аргументов (int, editorFrag)

Однако мой редакторFrag расширяет фрагмент, поэтому я путаюсь, почему это происходит. Ниже приведен мой код для файлов, о которых я упоминал. Любая помощь приветствуется.

Editor.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" />

editorFrag.java

public class editorFrag extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) 
    {

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.newlevel, container, false);
    }
}

MainActivity.java

public class editorActivity extends FragmentActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.editor);

        // Check that the activity is using the layout version with the fragment_container FrameLayout
        if(findViewById(R.id.fragment_container) != null)
        {
            // if we are being restored from a previous state, then we dont need to do anything and should
            // return or else we could end up with overlapping fragments.
            if(savedInstanceState != null)
                return;

            // Create an instance of editorFrag
            editorFrag firstFrag = new editorFrag();

            // add fragment to the fragment container layout
            getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFrag);
        }
    } 
}

Ответ:

Luksprog ответил на эту проблему для меня ниже, сказав мне проверить мой импорт. Eclipse решила импортировать версию SDK версии Fragment вместо той версии поддержки, которая мне нужна. Спасибо за помощь.

4b9b3361

Ответ 1

Вы забыли commit() свою транзакцию.

Ответ 2

Вы также забыли вызвать метод addtoBackStack(), иначе ваше приложение закрывается, когда вы нажмете кнопку "Назад".

Ответ 3

добавить commit() как это

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