搬到片段

时间:2017-01-21 16:24:44

标签: android android-fragments

我不确定我在这里有什么改变 我移动到WatchingFragment的代码运行正常。 我已经添加了代码来移动到新闻和buynsell片段。现在,当我从Nav菜单中单击该特定项目时。该应用程序崩溃。

任何帮助将不胜感激。

调用碎片

   @SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_home) {
        // Handle the camera action
    } else if (id == R.id.nav_watching) {
        WatchingFragment watchingFragment = new WatchingFragment();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayout_for_fragment, watchingFragment, watchingFragment.getTag()).commit();

    } else if (id == R.id.nav_news) {
        NewsFragment newsFragment = new NewsFragment();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayout_for_fragment, newsFragment, newsFragment.getTag()).commit();

    } else if (id == R.id.nav_buynsell) {
        BuynSellFragment buynSellFragment = new BuynSellFragment();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayout_for_fragment, buynSellFragment, buynSellFragment.getTag()).commit();

    } else if (id == R.id.nav_feedback) {

    } //else if (id == R.id.nav_send) {

    //}

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}

java.lang.IllegalArgumentException: No view found for id 0x7f0d0097 (uk.co.cyclesavy.cyclesavy:id/relativeLayout_for_fragment) for fragment NewsFragment{ba864be #0 id=0x7f0d0097}
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1292)
                  at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
                  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
                  at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998)
                  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:709)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

申请已终止。

片段的XML我试图导航到

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="uk.co.cyclesavy.cyclesavy.WatchingFragment">

        <RadioButton
        android:text="RadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="216dp"
        android:id="@+id/radioButton" />

    <CheckBox
        android:text="CheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/radioButton"
        android:layout_alignLeft="@+id/radioButton"
        android:layout_alignStart="@+id/radioButton"
        android:layout_marginBottom="71dp"
        android:id="@+id/checkBox" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radioButton"
        android:layout_alignRight="@+id/checkBox"
        android:layout_alignEnd="@+id/checkBox"
        android:layout_marginTop="30dp"
        android:id="@+id/button2" />

</FrameLayout>

1 个答案:

答案 0 :(得分:1)

当您调用replace时,您需要在布局中看到被片段替换的视图。在您的布局中没有带有该ID的视图。