Android上轮换一个片段"无法转换为"另一个片段

时间:2016-07-08 04:46:28

标签: java android android-fragments casting rotation

该应用程序正常工作,直到我旋转屏幕。然后这两个片段混淆了。 以下是活动的代码:

private PopupScreen popupFragment;
private PlayerWindow playerWindowFragment;

并在onCreate之后:

if (savedInstanceState == null) {
        Log.v(TAG, "onCreate: SAVEDINSTANCESTATE IS NULL, NEW ACTIVITY and FRAGMENTS");
        popupFragment = new PopupScreen();
        playerWindowFragment = new PlayerWindow();
        Bundle args = new Bundle();
        args.putInt("defaultBtnImgValue", au);
        args.putInt("btnImg", au);
        popupFragment.setArguments(args);
        playerWindowFragment.setArguments(args);
        FragmentTransaction transaction =
                getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.popupfragmentframe, popupFragment, POPUPWINDOWFRAME);
        transaction.replace(R.id.playerwindowfragmentframe, playerWindowFragment, PLAYERWINDOWFRAME);
        transaction.commit();
    } else {
        popupFragment = (PopupScreen) getSupportFragmentManager().findFragmentByTag(POPUPWINDOWFRAME);
        playerWindowFragment = (PlayerWindow) getSupportFragmentManager().findFragmentByTag(PLAYERWINDOWFRAME);
    }`

这是活动的xml。片段在LinearLayout中:

<FrameLayout
    android:id="@+id/popupfragmentframe"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</FrameLayout>
<FrameLayout
    android:id="@+id/playerwindowfragmentframe"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</FrameLayout>

这是日志:

Caused by: java.lang.ClassCastException: au.com.packagename.PlayerWindow cannot be cast to au.com.packagename.PopupScreen

当我在popupfragment xml中将Fragment夸大为FrameLayout而不是Activity时,就可以了。

感谢您的帮助。

0 个答案:

没有答案