0

当我从活动开始一个片段时,方法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();
    }
}

有谁知道为什么会这样?

4

1 回答 1

2

要解决这个问题:

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

有关更多信息,请参阅:由 <fragment> 没有绑定类的标记引起的 InflateException

于 2013-02-23T05:47:53.237 回答