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

时间:2014-01-11 14:51:45

标签: java android android-fragments

我正在尝试在activity ::

中设置两个简单片段

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


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;
    }   

}

FragmentSelExample.java

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;
    }

}

RootActivity.java

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_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>

日志 ::

01-11 20:12:19.989: E/AndroidRuntime(621): FATAL EXCEPTION: main
01-11 20:12:19.989: E/AndroidRuntime(621): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demofilechooserexample/com.example.demofilechooserndfragmentexample.fragments.RootActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f030005 (com.example.demofilechooserexample:layout/root_layout) for fragment FragmentFileChooserExample{45fbd5e0 #0 id=0x7f030005 FragFilChooserTag}
01-11 20:12:19.989: E/AndroidRuntime(621):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-11 20:12:19.989: E/AndroidRuntime(621):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-11 20:12:19.989: E/AndroidRuntime(621):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-11 20:12:19.989: E/AndroidRuntime(621):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-11 20:12:19.989: E/AndroidRuntime(621):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 20:12:19.989: E/AndroidRuntime(621):  at android.os.Looper.loop(Looper.java:123)
01-11 20:12:19.989: E/AndroidRuntime(621):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-11 20:12:19.989: E/AndroidRuntime(621):  at java.lang.reflect.Method.invokeNative(Native Method)
01-11 20:12:19.989: E/AndroidRuntime(621):  at java.lang.reflect.Method.invoke(Method.java:521)
01-11 20:12:19.989: E/AndroidRuntime(621):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-11 20:12:19.989: E/AndroidRuntime(621):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-11 20:12:19.989: E/AndroidRuntime(621):  at dalvik.system.NativeStart.main(Native Method)
01-11 20:12:19.989: E/AndroidRuntime(621): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f030005 (com.example.demofilechooserexample:layout/root_layout) for fragment FragmentFileChooserExample{45fbd5e0 #0 id=0x7f030005 FragFilChooserTag}
01-11 20:12:19.989: E/AndroidRuntime(621):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
01-11 20:12:19.989: E/AndroidRuntime(621):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-11 20:12:19.989: E/AndroidRuntime(621):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-11 20:12:19.989: E/AndroidRuntime(621):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
01-11 20:12:19.989: E/AndroidRuntime(621):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
01-11 20:12:19.989: E/AndroidRuntime(621):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
01-11 20:12:19.989: E/AndroidRuntime(621):  at android.app.Activity.performStart(Activity.java:3781)
01-11 20:12:19.989: E/AndroidRuntime(621):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
01-11 20:12:19.989: E/AndroidRuntime(621):  ... 11 more

1 个答案:

答案 0 :(得分:1)

您正在使用R.layout.root_layout

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

add()的第一个参数应该是您要添加id的{​​{1}}的{​​{1}},而不是实际的xml resoruce。尝试将其更改为:

ViewGroup