将活动转换为片段:黑屏和重新启动

时间:2017-02-21 01:22:01

标签: android xml android-fragments

我正在转换使用Activities的应用程序来代替使用Fragment。我将一个视图的xml更改为具有FrameLayout,我将放置片段。我得到的问题是当我尝试设置片段时,屏幕变黑并且#34;应用程序终止。"输出到控制台,应用程序重新启动。

您将找到MainActivity的代码片段,主要的XML和Fragment。任何关于为什么会发生这种情况的提示都会非常感激。

活动

        FragmentManager fragmentManager = getSupportFragmentManager();
        //Start the fragment
        Fragment gameManager = new GameManager();
        fragmentManager.beginTransaction()
                .replace(R.id.fragment_container,gameManager)
                .addToBackStack(null)
                .commit();

XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip" >

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></FrameLayout>

        <Button
            android:id="@+id/start"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/start" >
        </Button>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/options"
            android:textAppearance="?android:attr/textAppearanceMedium" >
        </TextView>

        <Spinner
            android:id="@+id/option_max_points"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:prompt="@string/option_max_points" >
        </Spinner>

        <CheckBox
            android:id="@+id/mixed_trade"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/option_mixed_trade" >
        </CheckBox>

        <CheckBox
            android:id="@+id/auto_discard"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/option_auto_discard" >
        </CheckBox>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/player_red"
            android:textAppearance="?android:attr/textAppearanceMedium" >
        </TextView>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/name1"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:inputType="text"
                android:text="" >
            </EditText>

            <CheckBox
                android:id="@+id/human1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/human" >
            </CheckBox>
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/player_blue"
            android:textAppearance="?android:attr/textAppearanceMedium" >
        </TextView>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/name2"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:inputType="text"
                android:text="" >
            </EditText>

            <CheckBox
                android:id="@+id/human2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/human" >
            </CheckBox>
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/player_green"
            android:textAppearance="?android:attr/textAppearanceMedium" >
        </TextView>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/name3"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:inputType="text"
                android:text="" >
            </EditText>

            <CheckBox
                android:id="@+id/human3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/human" >
            </CheckBox>
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/player_orange"
            android:textAppearance="?android:attr/textAppearanceMedium" >
        </TextView>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/name4"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:inputType="text"
                android:text="" >
            </EditText>

            <CheckBox
                android:id="@+id/human4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/human" >
            </CheckBox>
        </LinearLayout>

        <Button
            android:id="@+id/reset"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/players_reset" >
        </Button>
    </LinearLayout>

</ScrollView>

片段

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    fa = (FragmentActivity) super.getActivity();

    CatAndroidApp app = (CatAndroidApp) getActivity().getApplicationContext();

    RelativeLayout frame = new RelativeLayout(getActivity());

    TextureManager texture = app.getTextureManagerInstance();

    if (texture == null) {
        texture = new TextureManager(getActivity().getResources());
        app.setTextureManagerInstance(texture);
    }

    //changed constructor
    view = new GameView(this,getActivity());
    renderer = new GameRenderer(view);
    view.setRenderer(renderer);
    view.requestFocus();
    view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT, 1));
    frame.addView(view);

    boolean horizontal = getActivity().getResources().getDisplayMetrics().widthPixels < getActivity().getResources().getDisplayMetrics().heightPixels;
    resources = new ResourceView(getActivity());
    resources.setOrientation(horizontal ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
    RelativeLayout.LayoutParams params;
    if (horizontal)
        params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    else
        params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
    params.addRule(horizontal ? RelativeLayout.ALIGN_PARENT_BOTTOM : RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    frame.addView(resources, params);

    board = app.getBoardInstance();


    turnHandler = new UpdateHandler();

    //return frame;
    return frame;
}

0 个答案:

没有答案