obj Fragment wrong 2nd argument type found 'Android.support.V4.app.Fragment.' required 'Android.app.Fragment'

前端 未结 4 1229
醉梦人生
醉梦人生 2020-12-30 21:50

I can\'t work out why my objFragment is giving me issues with the support manager, I\'m getting this error:

 Error:(101, 17) error: no suitable method found         


        
相关标签:
4条回答
  • 2020-12-30 21:56

    The problem is that there is a mismatch in fragments types between objFragment and fragmentManager. The first is from package android.support.v4.app while the second is from the android.app package.

    See this answer here to know the difference between the two types of Fragments.

    Now to fix the issue you have to handle your imports.

    Either use import android.app.Fragment instead of import android.support.v4.app.Fragment

    Or do as Modge suggested; Update the main content of your app with a support Fragment manager like this: FragmentManager fragmentManager = getSupportFragmentManager(). But then you also have to change the type of the FragmentManager itself; import android.app.FragmentManager to import android.support.v4.app.FragmentManager;

    0 讨论(0)
  • 2020-12-30 22:05

    Just change getFragmentManager() to getSupportFragmentManager()

    0 讨论(0)
  • 2020-12-30 22:06

    Below is the answer to a similar question I found while searching the internet. This worked for me, so I hope it will help you.

    Menu_Fragment is not inheriting from android.support.v4.app.Fragment. Presumably, it is inheriting from android.app.Fragment.

    There are two fragment implementations: the native one (e.g., android.app.Fragment) and the backport (e.g., android.support.v4.app.Fragment). You need to be consistent. Your activity is a FragmentActivity, which is part of the backport, so you need your fragments to inherit from android.support.v4.app.Fragment.

    0 讨论(0)
  • 2020-12-30 22:21

    You tried to use the non support FragmentManager for your transaction. Change it to get the support one.

        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, objFragment)
                .commit();
    
    0 讨论(0)
提交回复
热议问题