Android 尝试将片段添加到我的片段容器 FrameLayout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12114015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:46:34  来源:igfitidea点击:

Trying to add a fragment to my fragment container FrameLayout

androidandroid-layoutandroid-fragmentsandroid-framelayout

提问by Pedrom

I have created an xml file called editor.xml which contains a FrameLayout. In my main activity I am trying to add my custom fragment to my FrameLayout.

我创建了一个名为 editor.xml 的 xml 文件,其中包含一个 FrameLayout。在我的主要活动中,我试图将我的自定义片段添加到我的 FrameLayout。

The error I receive when trying to add my fragment is:

尝试添加片段时收到的错误是:

The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, editorFrag)

FragmentTransaction 类型中的 add(int, Fragment) 方法不适用于参数 (int, editorFrag)

However my editorFrag extends Fragment so I am confused on why this is happening. Below is my code for the files I have mentioned. Any help is appreciated.

但是我的 editorFrag 扩展了 Fragment 所以我很困惑为什么会发生这种情况。下面是我提到的文件的代码。任何帮助表示赞赏。

Editor.xml

编辑器.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

编辑器Frag.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

主活动.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);
        }
    } 
}

Answered:

回答:

Luksprog answered this problem for me below by telling me to check my imports. Eclipse chose to import the SDK version of Fragment instead of the support version that I needed. Thank you for the help.

Luksprog 在下面通过告诉我检查我的进口为我回答了这个问题。Eclipse 选择导入 Fragment 的 SDK 版本,而不是我需要的支持版本。感谢您的帮助。

回答by Marcin Orlowski

You forgot to commit()your transaction.

你忘记了commit()你的交易。

回答by seken1

You also forgot to call the addtoBackStack()method, otherwise your app closes when you hit the back button.

您还忘记调用该addtoBackStack()方法,否则当您点击后退按钮时您的应用程序将关闭。

回答by Milon

add commit() like this

像这样添加 commit()

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