SachinKS 2013-02-20 03:01 采纳率: 0%
浏览 17065
已结题

Fragment getArguments() 返回空

有一个Fragment其中有一个作为根布局的tabHost:

<?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>

创建和更新Fragment的代码:

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();
    }
}

EpgEventListFragmentonCreate(...)方法中,我在获取参数Bunble的时候总是得到null:

@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);

    ...
}

不知道问题在哪?

我还试过在onAttach(...)中获取getArguments(),但是结果都一样,返回null。

请各位大侠帮忙解决一下哦。

  • 写回答

4条回答

  • hewence1 2014-11-18 01:46
    关注

    fm.beginTransaction()
    .replace(placeholder, new EpgEventListFragment(), tabId)
    .commit();
    这里有问题, 你又new了一个fragment 那么EpgEventListFragment fragment = new EpgEventListFragment();
    fragment.setArguments(arguments); 这个fragment 完全没有用。
    你修改成
    fm.beginTransaction()
    .replace(placeholder, fragment , tabId)
    .commit();用该就OK了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?