尝试将片段添加到我的片段容器 FrameLayout [英] Trying to add a fragment to my fragment container FrameLayout

查看:21
本文介绍了尝试将片段添加到我的片段容器 FrameLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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

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

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.

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

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

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 选择导入 Fragment 的 SDK 版本,而不是我需要的支持版本.谢谢你的帮助.

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.

推荐答案

您忘记了commit()您的交易.

这篇关于尝试将片段添加到我的片段容器 FrameLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆