Java 尝试切换到其他片段时应用程序崩溃

Java 尝试切换到其他片段时应用程序崩溃,java,android,eclipse,android-fragments,Java,Android,Eclipse,Android Fragments,我有一个游戏应用程序,有两个不同的片段。第一个是欢迎/说明页面,带有“开始游戏”按钮。第二个是游戏本身,包含一个按钮,“新游戏”,该按钮应该会将用户带回原始片段。该按钮可以在游戏类中创建新游戏并删除所有分数,等等。但是当我添加代码切换回“开始片段”时,它崩溃了。提前谢谢!试图找到一个类似的问题,但运气不好,我也是android编程新手,所以任何解释都非常感谢 public class GameFragment extends Fragment implements OnClickListener

我有一个游戏应用程序,有两个不同的片段。第一个是欢迎/说明页面,带有“开始游戏”按钮。第二个是游戏本身,包含一个按钮,“新游戏”,该按钮应该会将用户带回原始片段。该按钮可以在游戏类中创建新游戏并删除所有分数,等等。但是当我添加代码切换回“开始片段”时,它崩溃了。提前谢谢!试图找到一个类似的问题,但运气不好,我也是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>

问题:

getActivity().setContentView(R.layout.game_fragment);
您正试图将活动布局更改为
game\u fragment
布局,但由于活动的布局已更改,因此该布局无法找到
.replace(R.id.rootLayout

解决方案:

getActivity().setContentView(R.layout.game_fragment);
删除
getActivity().setContentView(R.layout.game_fragment);
,这样在替换片段时仍然可以看到
R.id.rootLayout
视图

<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>
编辑:

getActivity().setContentView(R.layout.game_fragment);
换成这个

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

您实现main_activity.xml的方式是错误的

如果您有一个容器(即id:rootLayout),则不必在xml中指定片段。您可以通过编程方式添加/删除/替换它们(这样做时称为动态片段)。在您的示例中,您同时使用了静态片段和动态片段

静态是指使用
标记在xml中定义片段,而静态片段则是指您无法控制何时查看和隐藏片段。动态是指您拥有一个用作容器的布局,然后使用
片段管理器处理片段


并尝试发布OpeningFragment&MainActivity的代码

请显示活动的布局XML。对于这种类型的场景,最好将显示片段的
活动
监视器保留下来,这样当您需要片段时,可以将片段替换为另一个片段,例如:
FragmentA
by
FragmentB
but fragments和who Ask
FragmentB
出现在它的位置:因此你为活动片段创建了一个通信通道,说明她应该用
FragmentB
替换它。在游戏后,如果我这样做,程序在我尝试从e打开游戏片段。(现在只有当我已经从打开到游戏片段,然后尝试返回到打开时,它才会崩溃)@user3192682这是您问题的答案。如果您有其他问题,您可能应该问一个新问题。尝试使用view.而不是getActivity()。一开始,但这不起作用,程序立即崩溃。空指针异常,因为无法访问小部件。@user3192682您的设计很复杂。您实际上不需要更改活动的布局,而是使用膨胀的片段视图来获取视图(按钮)@Rod_Algonquin你能解释得更多吗?我要用教授给我们的一个例子。正如我说的,我是android新手,所以我不确定这意味着什么,因为我的代码过于复杂。非常感谢你的帮助