从ViewPager片段调用片段

时间:2016-07-11 17:35:54

标签: android

从标签片段调用Fragment时出错。 我有MainActivity使用TabFragment使用ViewPager。 在ViewPager中,我有4个选项卡。我正在调用第一个标签片段中的另一个片段。

MainActivity

 TabFragment tabFrag = new TabFragment();

    mFragmentManager = getSupportFragmentManager();
    mFragmentTransaction = mFragmentManager.beginTransaction();
    mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();

现在从TabFragment扩展Fragment我在不同的标签上调用了4个片段。

**  @Override
public Fragment getItem(int position){
    Fragment fragment=null;
    switch (position){
        case 0 : {
                  fragment= new PostFragment();
                  Bundle bundle = new Bundle();
                  bundle.putString("fragmentName","0");
                  fragment.setArguments(bundle);return fragment;
                 }

        case 1 : {fragment= new EventFragment();
                  Bundle bundle = new Bundle();
                  bundle.putString("fragmentName","1");
                  fragment.setArguments(bundle);return fragment;}
        case 2 : {fragment= new ServiceFragment();
                    Bundle bundle = new Bundle();
                    bundle.putString("fragmentName","2");
                    fragment.setArguments(bundle);return fragment;
                 }
        case 3 : {fragment= new AccountFragment();
                  Bundle bundle = new Bundle();
                  bundle.putString("fragmentName","3");
                  fragment.setArguments(bundle);return fragment;
        }
    }
    return fragment;
}

现在在PostFragment中,我在按钮点击时调用另一个片段PostQuestionFragment。

   public void onClick(View view) {
    int id = view.getId();
    switch (id){
        case R.id.post:


            animateFAB();
            break;
        case R.id.postQue:
        {

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            PostQuestionFragment postQuestionFragment = new PostQuestionFragment();
            fragmentTransaction.replace(R.id.containerView, postQuestionFragment);
            fragmentTransaction.addToBackStack(null);

            fragmentTransaction.commit();
            break;
        }


        case R.id.fab2:


            break;
    }
}

我在fragmentTransaction.replace上遇到错误(R.id.containerView,postQuestionFragment);以下是日志信息。

4836/com.lab.bankersbank E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.lab.bankersbank, PID: 14836
    java.lang.IllegalArgumentException: No view found for id 0x7f0c007f (com.lab.bankersbank:id/containerView) for fragment PostQuestionFragment{85a5fe #2 id=0x7f0c007f}
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
     at android.os.Handler.handleCallback(Handler.java:746)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:148)
     at android.app.ActivityThread.main(ActivityThread.java:5443)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

更新:

MainActivity.java

 private static final String VIEW_KEY1 ="key1" ;
private static final String VIEW_KEY2 ="key2" ;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton notifBtn = (FloatingActionButton) findViewById(R.id.post);
    FloatingActionButton postBtn = (FloatingActionButton) findViewById(R.id.post);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);


    TabFragment tabFrag = new TabFragment();
    mFragmentManager = getSupportFragmentManager();
    mFragmentTransaction = mFragmentManager.beginTransaction();
    mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();

}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();


    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {

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

activity_main.xml中

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

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="80dp"
    tools:openDrawer="start">

    <FrameLayout
        android:id="@+id/containerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

请帮忙。

由于 迪帕克

0 个答案:

没有答案