Android IllegalStateException片段在方向更改后未创建视图

Android IllegalStateException片段在方向更改后未创建视图,android,android-fragments,illegalstateexception,orientation-changes,Android,Android Fragments,Illegalstateexception,Orientation Changes,我有一个应用程序,它有一个用户可以从中选择项目的主要活动。该选择将显示一个片段(TracksActivityFragment),该片段本身就是另一个列表。当选择该列表中的一项时,将添加一个片段,该片段是对话框片段。到目前为止还不错,但当我旋转设备时,会调用affragment的onCreate(),然后调用DailogFragment的onCreate(),然后它会随着IllegalStateException在affragment的活动第20行(setContentView)死亡 下面是该活动

我有一个应用程序,它有一个用户可以从中选择项目的主要活动。该选择将显示一个片段(
TracksActivityFragment
),该片段本身就是另一个列表。当选择该列表中的一项时,将添加一个片段,该片段是
对话框片段
。到目前为止还不错,但当我旋转设备时,会调用affragment的
onCreate()
,然后调用
DailogFragment的
onCreate()
,然后它会随着
IllegalStateException
affragment的
活动第20行(
setContentView
)死亡

下面是该活动的一部分,其中有一行:

public class TracksActivity extends AppCompatActivity
{
    private String mArtistName;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tracks); //DIES HERE
下面是片段的onCreate

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

    if(savedInstanceState != null)
    {
        //We've got data saved. Reconstitute it
        mTrackRowItemList = savedInstanceState.getParcelableArrayList(KEY_ITEMS_LIST);
    }
}
对话框片段
轨迹片段
中创建,如下所示:

PlayerFragment fragment = PlayerFragment.newInstance(mTrackRowItemList, i, mArtistBitmapFilename);

// The device is smaller, so show the fragment fullscreen
FragmentTransaction transaction = fragMan.beginTransaction();

// For a little polish, specify a transition animation
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

// To make it fullscreen, use the 'content' root view as the container
// for the fragment, which is always the root view for the activity
transaction.add(android.R.id.content, fragment).addToBackStack(null).commit();
这是DialogFragment的onCreate

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

    if (savedInstanceState != null)
    {
        //We've got data saved. Reconstitute it
        if (mPlayer != null)
        {
            mPlayer.seekTo(savedInstanceState.getInt(KEY_SONG_POSITION));
        }
    }
}
不知道为什么它会返回到
TracksFragment
,因为它的
DialogFragment
在旋转时处于活动状态,但既然是这样,我似乎需要重新创建整个
DialogPlayer
对象,但在调用它的
onCreate
时,它似乎会保持这个状态


有人知道这里需要做什么吗?

好的,以前有人问过这个问题,但我没有考虑解决方案……我不应该这样做

出于某种原因,Android希望Tracks布局XML使用FrameLayout而不是片段


所以,只要在布局xml文件中用FrameLayout替换片段,一切都很好。

替换的是什么?你能把原来的布局贴到问题上吗?我没有用FrameLayout替换片段,我只是将片段包装在FrameLayout中,这修复了屏幕方向更改异常。我想我从来没有猜到这是原因,在这种情况下,异常消息几乎是无用的。我只是在主布局xml中替换了单词“