gpt4 book ai didi

java - 尝试切换到不同 fragment 时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 02:47:04 25 4
gpt4 key购买 nike

我有一个包含 2 个不同 fragment 的游戏应用程序。第一个是带有“开始游戏”按钮的欢迎/说明页面。第二个是游戏本身,包含一个按钮“新游戏”,可以将用户带回原始 fragment 。就在游戏类中创建新游戏和清除所有分数等而言,该按钮工作正常,但是当我添加代码以切换回“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
}

}

这是主要 Activity 和游戏 fragment 的布局 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>

这是游戏 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 投诉

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

您正在尝试将 Activity 布局的布局更改为 game_fragment 布局,到那时将找不到 .replace(R.id.rootLayout 因为您的布局 Activity 已更改。

解决方法:

删除 getActivity().setContentView(R.layout.game_fragment); 这样当您替换 fragment 时您仍然拥有 R.id.rootLayout View 。

编辑:

改成这个

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

关于java - 尝试切换到不同 fragment 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24846096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com