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条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题