[英]App crashing when trying to switch to different fragment
I have a game application that has 2 different fragments. 我有一个包含2个不同片段的游戏应用程序。 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. 第二个是游戏本身,其中包含一个按钮“ New Game”,该按钮应将用户带回到原始片段。 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. 该按钮在游戏类中创建一个新游戏并清除所有分数等方面都可以正常工作,但是当我添加代码以切换回“ Opening Fragment”时,它崩溃了。 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! 试图找到一个类似的问题,但没有运气,这也是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
}
}
here is the layout XML for the main activity and the game 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>
and this is the game fragment xml 这是游戏片段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 下面的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)
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. 您正在尝试将活动布局的布局更改为game_fragment
布局,该布局到game_fragment
将找不到.replace(R.id.rootLayout
因为您的活动布局已更改。
solution: 解:
remove the getActivity().setContentView(R.layout.game_fragment);
删除getActivity().setContentView(R.layout.game_fragment);
so you still have the R.id.rootLayout
view when you replace a the fragment. 因此,当您替换片段时,您仍然具有R.id.rootLayout
视图。
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 您实现main_activity.xml的方式错误
If you have a container (ie your id: rootLayout) you don't have to specify the fragments in the xml. 如果您有容器(即您的ID:rootLayout),则无需在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. 静态是指使用<fragment ../>
标签在xml中定义片段时,使用静态片段时无法控制何时查看和隐藏片段。 Dynamic is when you have a layout which works as a container, and then you handle the fragments with the FragmentManager
动态是指当您具有可以用作容器的布局,然后使用FragmentManager
处理FragmentManager
And try post your code for the OpeningFragment & MainActivity 并尝试发布您的OpeningFragment和MainActivity代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.