将片段添加到另一个片段

时间:2016-04-19 12:22:56

标签: android

我试图将片段添加到另一个片段,但我得到以下异常:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.domain.app/com.domain.ui.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0d00a1 (com.domain.app:id/container) for fragment FrontFragment{259d9bc #0 id=0x7f0d00a1}
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
 at android.app.ActivityThread.-wrap11(ActivityThread.java)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5417)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0d00a1 (com.domain.app:id/container) for fragment FrontFragment{259d9bc #0 id=0x7f0d00a1}

活动布局:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment"
    android:name=".MainFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout="@layout/main_fragment" />

片段布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

我如何尝试从MainFragment添加子片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.main_fragment, container, false);

    // If there is no saved instance state, add a fragment representing the
    // front of the card to this activity. If there is saved instance state,
    // this fragment will have already been added to the activity.
    getChildFragmentManager()
            .beginTransaction()
            .add(R.id.container, new PassbookViewerFrontFragment())
            .commit();

    return rootView;
}

据我所知,我提供给FragmentManager的id存在并且应该存在。关于我可能做错的任何提示?

3 个答案:

答案 0 :(得分:0)

当你尝试添加嵌套片段时,你夸大了layout-resource,但是当时没有创建视图。替换此代码:

getChildFragmentManager()
        .beginTransaction()
        .add(R.id.container, new PassbookViewerFrontFragment())
        .commit();

另一种方法,在onCreateView

之后调用

答案 1 :(得分:0)

您的 Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder() .setDataTypes(DataType.TYPE_SPEED) .setDataSourceTypes(DataSource.TYPE_DERIVED) .build()) .setResultCallback(new ResultCallback<DataSourcesResult>() { @Override public void onResult(DataSourcesResult dataSourcesResult) { Log.i(TAG, "Result: " + dataSourcesResult.getStatus().toString()); for (DataSource dataSource : dataSourcesResult.getDataSources()) { Log.i(TAG, "Data source found: " + dataSource.toString()); } } }); 必须在您的FrameLayout中才能生效。这是因为您要放置子片段的activity layout必须先填充,然后才能填充它。通过将其包含在FrameLayout布局中,当调用main_fragment.xml时,框架布局也会被充气,然后就可以填充子片段了。

简而言之,删除子片段布局xml文件,然后将其放在main_fragment布局中:

inflater.inflate(R.layout.main_fragment)

答案 2 :(得分:0)

问题在于,在活动布局中,我已经使用id声明了lf并且发生了此ID,而不是我在片段布局上使用的.gitattributes ID。 短篇简短:要么<fragment id="@+id/fragment">上没有id,要么片段中有包装布局。

另外,在container内部进行FragmentTransaction工作。