片段getArguments()返回null片段、getArguments、null

2023-09-07 15:45:34 作者:我与孤独相伴

我有一个片段具有 TabHost 作为根布局如下...

I have a Fragment which has a TabHost as the root layout as follows...

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <FrameLayout
                android:id="@+id/tab_1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />

            <!-- More FrameLayouts here - each are placeholders for Fragments -->    

        </FrameLayout>
    </LinearLayout>
</TabHost>

在code创建/更新每个片段的标签内容如下...

The code to create / update each Fragment for the tab content is as follows...

private void updateTab(String tabId, int placeholder) {
    FragmentManager fm = getFragmentManager();
    if (fm.findFragmentByTag(tabId) == null) {
        Bundle arguments = new Bundle();
        arguments.putInt("current_day", mCurrentTab);
        EpgEventListFragment fragment = new EpgEventListFragment();
        fragment.setArguments(arguments);

        fm.beginTransaction()
                .replace(placeholder, new EpgEventListFragment(), tabId)
                .commit();
    }
}

EpgEventListFragment 然后我试图让参数捆绑,但我总是执行以下操作...

In the onCreate(...) method of the EpgEventListFragment I then try to get the arguments Bundle but I always get null doing the following...

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle arguments = getArguments();
    if (arguments == null)
        Toast.makeText(getActivity(), "Arguments is NULL", Toast.LENGTH_LONG).show();
    else
        mCurrentDay = getArguments().getInt("current_day", 0);

    ...
}

我缺少的是在这里吗?我也试过 getArguments() onAttach(...),但我还是得到了空。我是新来使用片段所以我希望有一个简单的原因,但在搜索时我还没有拿出任何东西。

What am I missing here? I also tried getArguments() in onAttach(...) but I still got null. I'm new to using Fragments so I'm hoping there's a simple reason but I haven't come up with anything when searching.

推荐答案

我想这与你的问题做的:

I'm thinking this has to do with your problem:

fm.beginTransaction()
    .replace(placeholder, new EpgEventListFragment(), tabId)
    .commit();

您正在做一个新的片段(不带参数,因为它已经新鲜实例化)。

You're making a new Fragment (that doesn't have arguments since it's been freshly instantiated).

而不是尝试

Fragment fragment = new EpgEventListFragment();
fragment.setArguments(arguments);
fm.beginTransaction()
    .replace(placeholder, fragment, tabId)
    .commit();