Error: required: android:support.v4.FragmentManager, found: android.app.FragmentManager

前端 未结 4 523
无人及你
无人及你 2021-01-22 12:47
 FragmentManager manager = getFragmentManager();

It is giving the error

required : android:support.v4.FragmentManager
foun

相关标签:
4条回答
  • 2021-01-22 13:38

    This solved the problem for me for creating fragment with viewpager.

    Make your activity extend AppCompatActivity(), then use supportFragmentManager.

    abstract class YourActivity() : AppCompatActivity() { /*your Activity Code*/ }
    

    Then, when creating your viewpager with fragment:

     val adapter = ViewPagerAdapter(supportFragmentManager)
     viewPager.adapter = adapter
     sliding_tabs.setupWithViewPager(viewPager)
    
    0 讨论(0)
  • 2021-01-22 13:40

    add these two import

      import android.support.v4.app.FragmentManager;
        import android.support.v4.app.FragmentTransaction;
    

    for example

       FragmentA fragmentA = new FragmentA();
        FragmentManager fragmentManager =getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.id_parent_fragment,fragmentA);
        fragmentTransaction.commit();
    

    consider FragementA is the fragment that you want to insert in the activity which in this case is the container in the activity id_parent_fragment

    0 讨论(0)
  • 2021-01-22 13:43

    Use getSupportFragmentManager(), or change the import for FragmentManager from what you have (android.support.v4.app.FragmentManager) to android.app.FragmentManager.

    You need to consistently use either the native classes or the backport. You cannot mix and match.

    0 讨论(0)
  • 2021-01-22 13:50

    change the import of Fragment Activity below

     import android.support.app.FragmentActivity;
    

    to

     import android.support.v4.app.FragmentActivity;
    
    0 讨论(0)
提交回复
热议问题