Java 在单个活动中设置两个片段

Java 在单个活动中设置两个片段,java,android,android-fragments,Java,Android,Android Fragments,我试图在活动中设置两个简单片段:: 我面临如图所示的日志错误,如何解决此问题 FragmentFileChooserExample.java public class FragmentFileChooserExample extends Fragment{ //function is used to link fragment file(frg_layout_file_chooser.xml) to FragmentFileChooserExample @Override

我试图在活动中设置两个简单片段::

我面临如图所示的日志错误,如何解决此问题


FragmentFileChooserExample.java

public class FragmentFileChooserExample extends Fragment{

    //function is used to link fragment file(frg_layout_file_chooser.xml) to FragmentFileChooserExample
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View objVw=inflater.inflate(R.layout.frg_layout_file_chooser_part, container, false);

        return objVw;
    }   

}
public class FragmentSelExample extends Fragment{

    //function is used to link fragment file(frg_layout_fragment_chooser.xml) to FragmentSelExample
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View objVw=inflater.inflate(R.layout.frg_layout_frag_chooser_part, container, false);

        return objVw;
    }

}
public class RootActivity extends FragmentActivity{

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);

        setContentView(R.layout.root_layout);

        FragmentFileChooserExample fileChEx=new FragmentFileChooserExample();
        FragmentSelExample frgSlEx=new FragmentSelExample();

        FragmentManager frgMng=getSupportFragmentManager();
        FragmentTransaction frgTrns=frgMng.beginTransaction();

        frgTrns.add(R.layout.root_layout, fileChEx, "FragFilChooserTag");
        frgTrns.add(R.layout.root_layout, frgSlEx, "FragFrgChooserTag");

        frgTrns.commit();


    }

}
FragmentSelExample.java

public class FragmentFileChooserExample extends Fragment{

    //function is used to link fragment file(frg_layout_file_chooser.xml) to FragmentFileChooserExample
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View objVw=inflater.inflate(R.layout.frg_layout_file_chooser_part, container, false);

        return objVw;
    }   

}
public class FragmentSelExample extends Fragment{

    //function is used to link fragment file(frg_layout_fragment_chooser.xml) to FragmentSelExample
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View objVw=inflater.inflate(R.layout.frg_layout_frag_chooser_part, container, false);

        return objVw;
    }

}
public class RootActivity extends FragmentActivity{

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);

        setContentView(R.layout.root_layout);

        FragmentFileChooserExample fileChEx=new FragmentFileChooserExample();
        FragmentSelExample frgSlEx=new FragmentSelExample();

        FragmentManager frgMng=getSupportFragmentManager();
        FragmentTransaction frgTrns=frgMng.beginTransaction();

        frgTrns.add(R.layout.root_layout, fileChEx, "FragFilChooserTag");
        frgTrns.add(R.layout.root_layout, frgSlEx, "FragFrgChooserTag");

        frgTrns.commit();


    }

}
RootActivity.java

public class FragmentFileChooserExample extends Fragment{

    //function is used to link fragment file(frg_layout_file_chooser.xml) to FragmentFileChooserExample
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View objVw=inflater.inflate(R.layout.frg_layout_file_chooser_part, container, false);

        return objVw;
    }   

}
public class FragmentSelExample extends Fragment{

    //function is used to link fragment file(frg_layout_fragment_chooser.xml) to FragmentSelExample
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View objVw=inflater.inflate(R.layout.frg_layout_frag_chooser_part, container, false);

        return objVw;
    }

}
public class RootActivity extends FragmentActivity{

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);

        setContentView(R.layout.root_layout);

        FragmentFileChooserExample fileChEx=new FragmentFileChooserExample();
        FragmentSelExample frgSlEx=new FragmentSelExample();

        FragmentManager frgMng=getSupportFragmentManager();
        FragmentTransaction frgTrns=frgMng.beginTransaction();

        frgTrns.add(R.layout.root_layout, fileChEx, "FragFilChooserTag");
        frgTrns.add(R.layout.root_layout, frgSlEx, "FragFrgChooserTag");

        frgTrns.commit();


    }

}
root\u layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@+id/layout_filechooser_contID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.49" >

        </FrameLayout>

        <FrameLayout
            android:id="@+id/layout_fragment_contID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.54" >

        </FrameLayout>
    </LinearLayout>

</RelativeLayout>

您在这里使用的是
R.layout.root\u布局

frgTrns.add(R.layout.root_layout, fileChEx, "FragFilChooserTag");
frgTrns.add(R.layout.root_layout, frgSlEx, "FragFrgChooserTag");
add()
的第一个参数应该是要添加
片段的
视图组的
id
,而不是实际的xml解析。尝试将其更改为:

frgTrns.add(R.id.layout_filechooser_contID, fileChEx, "FragFilChooserTag");
frgTrns.add(R.id.layout_fragment_contID, frgSlEx, "FragFrgChooserTag");