简体   繁体   中英

App crashing when trying to switch to different fragment

I have a game application that has 2 different fragments. The first is a welcome/instructions page with a button to, "Start Game". The second is the game itself, containing a button, "New Game" which is supposed to take the user back to the original fragment. The button works fine as far as creating a new game in the game class and erasing all scores, etc. but when I add the code to switch back to the "Opening Fragment" it crashes. Thanks in advance! Tried to find a similar question but didn't have luck, also am new to android programming so any explanations are always appreciated!

public class GameFragment extends Fragment implements OnClickListener {
    private Button newGame;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState){
            View view = inflater.inflate(R.layout.game_fragment, container, false);
            getActivity().setContentView(R.layout.game_fragment);

            newGame = (Button) getActivity().findViewById(R.id.newGameButton);
            newGame.setOnClickListener(this);

            return view;
    }

    public void onClick(View v) {
        ...
        case R.id.newGameButton:
            // Replace GameFragment with OpeningFragment
            getActivity().getFragmentManager().beginTransaction()
            .replace(R.id.rootLayout, new OpeningFragment()).commit();
            break;
            // rootLayout is the id of the main activity layout
    }

}

here is the layout XML for the main activity and the game fragment.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment android:name="com.example.pig.OpeningFragment"
              android:id="@+id/opening_fragment"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

    <fragment android:name="com.example.pig.GameFragment"
              android:id="@+id/game_fragment"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

</RelativeLayout>

and this is the game fragment xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/piggy" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

        <TextView
            android:id="@+id/player1Label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/player_1_label" />

        <TextView
            android:id="@+id/spaces12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/spaces12" />

        <TextView
            android:id="@+id/player2Label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/player_2_label" />

    </LinearLayout>

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

        <TextView
            android:id="@+id/player1NameView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/player_1_name_view" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/spaces12" />

        <TextView
            android:id="@+id/player2NameView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/player_2_name_view" />

    </LinearLayout>

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

        <TextView
            android:id="@+id/scoreLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/score" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/spaces16" />

        <TextView
            android:id="@+id/score2Label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/score" />

    </LinearLayout>

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

        <TextView
            android:id="@+id/player1ScoreView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/player_1_score_view" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/spaces24" />

        <TextView
            android:id="@+id/player2ScoreView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/player_2_score_view" />

    </LinearLayout>

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

        <TextView
            android:id="@+id/currentPlayerNameView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/current_player_name_view" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/turn_label" />

    </LinearLayout>

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

        <TextView
            android:id="@+id/pointsLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/points_label" />

        <TextView
            android:id="@+id/currentPlayerPointsView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/curr_points" />

    </LinearLayout>

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

        <ImageView
            android:id="@+id/dieImage"
            android:layout_width="71dp"
            android:layout_height="72dp"
            android:contentDescription="@string/die_image"
            android:src="@drawable/die1" />

    </LinearLayout>

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

        <Button
            android:id="@+id/endTurnButton"
            style="android:buttonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/end_turn" />

        <Button
            android:id="@+id/rollDieButton"
            style="android:buttonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/roll_die" />

    </LinearLayout>

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

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

    </LinearLayout>

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

        <TextView
            android:id="@+id/winner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/winner" />

        <TextView
            android:id="@+id/winsLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/wins" />

    </LinearLayout>

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

        <ImageButton
            android:id="@+id/diePic"
            android:layout_width="72dp"
            android:layout_height="61dp"
            android:contentDescription="@string/die_pic"
            android:src="@drawable/die1" />

    </LinearLayout>

</LinearLayout>

</FrameLayout>

Logcat complaints below

07-20 01:27:20.982: E/Trace(1034): error opening trace file: No such file or directory (2)
07-20 01:27:23.146: E/AndroidRuntime(1034): FATAL EXCEPTION: main
07-20 01:27:23.146: E/AndroidRuntime(1034): java.lang.IllegalArgumentException: No view found for id 0x7f060000 for fragment OpeningFragment{535b6a9c #1 id=0x7f060000}
07-20 01:27:23.146: E/AndroidRuntime(1034): at   android.app.FragmentManagerImpl.moveToState(FragmentManager.java:823)
07-20 01:27:23.146: E/AndroidRuntime(1034): at   android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
07-20 01:27:23.146: E/AndroidRuntime(1034): at android.app.BackStackRecord.run(BackStackRecord.java:635)
07-20 01:27:23.146: E/AndroidRuntime(1034):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
07-20 01:27:23.146: E/AndroidRuntime(1034):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
07-20 01:27:23.146: E/AndroidRuntime(1034):     at android.os.Handler.handleCallback(Handler.java:615)
07-20 01:27:23.146: E/AndroidRuntime(1034):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-20 01:27:23.146: E/AndroidRuntime(1034):     at android.os.Looper.loop(Looper.java:137)
07-20 01:27:23.146: E/AndroidRuntime(1034):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-20 01:27:23.146: E/AndroidRuntime(1034):     at java.lang.reflect.Method.invokeNative(Native Method)
07-20 01:27:23.146: E/AndroidRuntime(1034):     at java.lang.reflect.Method.invoke(Method.java:511)
07-20 01:27:23.146: E/AndroidRuntime(1034):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-20 01:27:23.146: E/AndroidRuntime(1034):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-20 01:27:23.146: E/AndroidRuntime(1034):     at dalvik.system.NativeStart.main(Native Method)

problem:

getActivity().setContentView(R.layout.game_fragment);

You are trying to change the layout of your activty layout to game_fragment layout which by then wont found the .replace(R.id.rootLayout because the layout of your activity is changed.

solution:

remove the getActivity().setContentView(R.layout.game_fragment); so you still have the R.id.rootLayout view when you replace a the fragment.

EDIT:

change it to this.

newGame = (Button) view.findViewById(R.id.newGameButton);
newGame.setOnClickListener(this);

The way you have implementet your main_activity.xml is wrong

If you have a container (ie your id: rootLayout) you don't have to specify the fragments in the xml. You add/remove/replace them programmatically ( when doing so it's called dynamic fragments). In your example you have used both static and dynamic fragments.

Static is when you define the fragment in xml with <fragment ../> tags, with static fragments you dont have control over when to view and hide your fragment. Dynamic is when you have a layout which works as a container, and then you handle the fragments with the FragmentManager

And try post your code for the OpeningFragment & MainActivity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM