Android ListFragment调用了两次

时间:2013-02-22 23:13:54

标签: android android-fragments android-listfragment android-fragmentactivity

当我从活动中启动片段时,方法 onCreate() onViewCreated()会被调用两次。

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chapter_list);

    FragmentManager frgManager = getSupportFragmentManager();
    if (findViewById(R.id.fl_chapter_detail) != null)
    {
        mbThreePaneLayout = true;
        FragmentChapterList frgChapterList = (FragmentChapterList) frgManager.findFragmentById(R.id.frg_chapter_list);
        frgChapterList.setActivateOnItemClick(true);
    }

    if (savedInstanceState == null)
    {
        String strSelectedSection = getIntent().getStringExtra(Konstant.KEY_SELECTED_SECTION);

        if (((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) != Configuration.SCREENLAYOUT_SIZE_XLARGE) || (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT))
            currentActionBar.setTitle(strSelectedSection);

        Bundle arguments = new Bundle();
        arguments.putString(Konstant.KEY_SELECTED_SECTION, strSelectedSection);

        FragmentChapterList frgChapterList = new FragmentChapterList();
        frgChapterList.setArguments(arguments);

        FragmentTransaction frgTransaction = frgManager.beginTransaction();
        frgTransaction.add(R.id.frg_chapter_list, frgChapterList).commit();
    }
}

有谁知道为什么会这样?

1 个答案:

答案 0 :(得分:2)

解决此问题:

  • 从< fragment />更改片段的标记类型到占位符,例如< FrameLayout />。
  • 删除name属性。

有关详细信息,请参阅:InflateException Caused by <fragment> Tag Without a Bound Class