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

查看:194
本文介绍了试图片段添加到我的片段容器的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,片段)是不适用的参数(INT,editorFrag)

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

不过我editorFrag延伸片段让我很困惑,为什么这种情况正在发生。下面是我的$ C $下我所提到的文件。任何帮助是AP preciated。

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.

Editor.xml

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

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中选择要导入的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.

推荐答案

您忘了提交()您的交易。

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

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