簡體   English   中英

嘗試切換到其他片段時應用崩潰

[英]App crashing when trying to switch to different fragment

我有一個包含2個不同片段的游戲應用程序。 第一個是帶有“開始游戲”按鈕的歡迎/說明頁面。 第二個是游戲本身,其中包含一個按鈕“ New Game”,該按鈕應將用戶帶回到原始片段。 該按鈕在游戲類中創建一個新游戲並清除所有分數等方面都可以正常工作,但是當我添加代碼以切換回“ Opening Fragment”時,它崩潰了。 提前致謝! 試圖找到一個類似的問題,但沒有運氣,這也是android編程的新手,所以總是對任何解釋都很感激!

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
    }

}

這是主要活動和游戲片段的布局XML。

<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>

這是游戲片段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投訴

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)

問題:

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

您正在嘗試將活動布局的布局更改為game_fragment布局,該布局到game_fragment將找不到.replace(R.id.rootLayout因為您的活動布局已更改。

解:

刪除getActivity().setContentView(R.layout.game_fragment); 因此,當您替換片段時,您仍然具有R.id.rootLayout視圖。

編輯:

更改為此。

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

您實現main_activity.xml的方式錯誤

如果您有容器(即您的ID:rootLayout),則無需在xml中指定片段。 您可以通過編程方式添加/刪除/替換它們(這樣做時稱為動態片段)。 在您的示例中,您同時使用了靜態和動態片段。

靜態是指使用<fragment ../>標簽在xml中定義片段時,使用靜態片段時無法控制何時查看和隱藏片段。 動態是指當您具有可以用作容器的布局,然后使用FragmentManager處理FragmentManager

並嘗試發布您的OpeningFragment和MainActivity代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM