[英]App keeps crashing when trying to change the state of a FloatingActionButton in a reloaded Fragment
[英]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.