以编程方式将片段添加到 android 中的框架布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20455416/
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 02:54:08  来源:igfitidea点击:

Programmatically adding fragment to framelayout in android

androidfragmentandroid-framelayout

提问by Mike Baxter

I am trying to build a UI combining both static and dynamic elements. For this, I have divided my activity into fragments - all app navigation is then done by replacing fragments instead of navigating between activities.

我正在尝试构建一个结合静态和动态元素的 UI。为此,我将我的活动划分为片段 - 然后通过替换片段而不是在活动之间导航来完成所有应用导航。

In my main activity layout, I am using a FrameLayout:

在我的主要活动布局中,我使用了一个FrameLayout

<FrameLayout
        android:id="@+id/mainframe"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_below="@id/topsection"
        android:layout_above="@id/lowersection" />

I have a fragment declared as such:

我有一个这样声明的片段:

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.fragmentlayout, container, false);
    }
}

Then, in my main activity (which extends FragmentActivity and uses the import android.support.v4.app.FragmentActivity, I am attempting to load this fragment into the frame layout.

然后,在我的主要活动(扩展 FragmentActivity 并使用 import )中android.support.v4.app.FragmentActivity,我试图将此片段加载到框架布局中。

MyFragment myf = new MyFragment();

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.mainframe, myf);
transaction.commit();

I have followed this from many other examples, however I am receiving a compiler error on the transaction.add()command, which nobody else seems to have encountered.

我已经从许多其他示例中遵循了这一点,但是我在transaction.add()命令中收到了编译器错误,其他人似乎没有遇到过。

The error I am receiving is: The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment).

我收到的错误是:The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment)

Why is this? The MyFragmentclass extends Fragmentso I would've thought this would work. What am I doing wrong?

为什么是这样?在MyFragment类扩展Fragment,所以我会一直认为这是可行的。我究竟做错了什么?

Edit: The imports for my main activity are:

编辑:我的主要活动的进口是:

import org.joda.time.DateTime;
import android.app.FragmentTransaction;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

回答by Steve Benett

Check your imports. Use android.support.v4.app.FragmentTransactioninstead of android.app.FragmentTransaction.

检查您的进口。使用android.support.v4.app.FragmentTransaction代替android.app.FragmentTransaction

Furthermore be sure you are using android.support.v4.app.Fragmentand calling getSupportFragmentManager(). It's easy to miss this calls / imports. Thx to saiful103a with the hint of the FragmentManager.

此外,请确保您正在使用android.support.v4.app.Fragment和调用getSupportFragmentManager(). 很容易错过这个调用/导入。用 FragmentManager 的提示向 saiful103a 表示感谢。