Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将活动转换为片段:黑屏并重新启动_Android_Xml_Android Fragments - Fatal编程技术网

Android 将活动转换为片段:黑屏并重新启动

Android 将活动转换为片段:黑屏并重新启动,android,xml,android-fragments,Android,Xml,Android Fragments,我正在将使用活动的应用程序转换为使用片段。我更改了一个视图的xml,使其具有一个框架布局,我将在其中放置片段。我遇到的问题是,当我试图设置片段时,屏幕变黑,“应用程序终止”。输出到控制台,应用程序重新启动 您将找到我的MainActivity、主XML和片段的代码片段。任何关于为什么会发生这种情况的提示都将不胜感激 活动 FragmentManager fragmentManager = getSupportFragmentManager(); //Start t

我正在将使用活动的应用程序转换为使用片段。我更改了一个视图的xml,使其具有一个框架布局,我将在其中放置片段。我遇到的问题是,当我试图设置片段时,屏幕变黑,“应用程序终止”。输出到控制台,应用程序重新启动

您将找到我的MainActivity、主XML和片段的代码片段。任何关于为什么会发生这种情况的提示都将不胜感激

活动

        FragmentManager fragmentManager = getSupportFragmentManager();
        //Start the fragment
        Fragment gameManager = new GameManager();
        fragmentManager.beginTransaction()
                .replace(R.id.fragment_container,gameManager)
                .addToBackStack(null)
                .commit();
XML


碎片

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    fa = (FragmentActivity) super.getActivity();

    CatAndroidApp app = (CatAndroidApp) getActivity().getApplicationContext();

    RelativeLayout frame = new RelativeLayout(getActivity());

    TextureManager texture = app.getTextureManagerInstance();

    if (texture == null) {
        texture = new TextureManager(getActivity().getResources());
        app.setTextureManagerInstance(texture);
    }

    //changed constructor
    view = new GameView(this,getActivity());
    renderer = new GameRenderer(view);
    view.setRenderer(renderer);
    view.requestFocus();
    view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT, 1));
    frame.addView(view);

    boolean horizontal = getActivity().getResources().getDisplayMetrics().widthPixels < getActivity().getResources().getDisplayMetrics().heightPixels;
    resources = new ResourceView(getActivity());
    resources.setOrientation(horizontal ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
    RelativeLayout.LayoutParams params;
    if (horizontal)
        params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    else
        params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
    params.addRule(horizontal ? RelativeLayout.ALIGN_PARENT_BOTTOM : RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    frame.addView(resources, params);

    board = app.getBoardInstance();


    turnHandler = new UpdateHandler();

    //return frame;
    return frame;
}
创建视图时的公共视图(布局、充气机、视图组容器、捆绑包保存状态){ fa=(FragmentActivity)super.getActivity(); CatAndroidApp=(CatAndroidApp)getActivity().getApplicationContext(); RelativeLayout框架=新的RelativeLayout(getActivity()); TextureManager纹理=app.getTextureManagerInstance(); 如果(纹理==null){ 纹理=新的纹理管理器(getActivity().getResources()); app.settexturemanager实例(纹理); } //更改的构造函数 视图=新游戏视图(这个,getActivity()); 渲染器=新游戏渲染器(视图); view.setRenderer(渲染器); view.requestFocus(); view.setLayoutParams(新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父级, LinearLayout.LayoutParams.MATCH_PARENT,1)); frame.addView(视图); 布尔水平=getActivity().getResources().getDisplayMetrics().widthPixels发布您的错误log@TruongHieu谢谢你的评论。从字面上说,控制台上打印的唯一内容是“应用程序终止”,您确定您已经检查了“logcat”选项卡以及控制台吗?如果是,请放置一些调试语句,以便您可以看到它的作用——onCreateView甚至被调用了吗?
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    fa = (FragmentActivity) super.getActivity();

    CatAndroidApp app = (CatAndroidApp) getActivity().getApplicationContext();

    RelativeLayout frame = new RelativeLayout(getActivity());

    TextureManager texture = app.getTextureManagerInstance();

    if (texture == null) {
        texture = new TextureManager(getActivity().getResources());
        app.setTextureManagerInstance(texture);
    }

    //changed constructor
    view = new GameView(this,getActivity());
    renderer = new GameRenderer(view);
    view.setRenderer(renderer);
    view.requestFocus();
    view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT, 1));
    frame.addView(view);

    boolean horizontal = getActivity().getResources().getDisplayMetrics().widthPixels < getActivity().getResources().getDisplayMetrics().heightPixels;
    resources = new ResourceView(getActivity());
    resources.setOrientation(horizontal ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
    RelativeLayout.LayoutParams params;
    if (horizontal)
        params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    else
        params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
    params.addRule(horizontal ? RelativeLayout.ALIGN_PARENT_BOTTOM : RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    frame.addView(resources, params);

    board = app.getBoardInstance();


    turnHandler = new UpdateHandler();

    //return frame;
    return frame;
}