incompatible types: HomeFragment cannot be converted to Fragment in Android

后端 未结 9 1590
轻奢々
轻奢々 2020-12-01 15:57

I\'m getting an error in this part of code:

private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragm         


        
相关标签:
9条回答
  • 2020-12-01 16:03

    Try changing

    import android.app.Fragment;

    to

    import android.support.v4.app.Fragment;

    Use classes from that support lib for all other imports too. Also getSupportFragmentManager() as mentioned in the other answer.

    0 讨论(0)
  • 2020-12-01 16:13

    In your HomeFragment class

    replace:

    import android.app.Fragment;
    

    with:

    import android.support.v4.app.Fragment;
    
    0 讨论(0)
  • 2020-12-01 16:14

    In Android Studio 2.3 getSupportFragmentManager works with android.support.v4.app but android studio 3.1 you have to use getFragmentManager enter image description here

    0 讨论(0)
  • 2020-12-01 16:16

    In my case i have changed line-1 with line-2

    Line-1: import android.app.Fragment;

    Line-2: import android.support.v4.app.Fragment;

    Its working

    0 讨论(0)
  • 2020-12-01 16:18

    This seems to be an import problem.

    When using getFragmentMangager(), make sure that your Fragment classes extend android.app.Fragment class.

    If by any chance you are using android.support.v4.app.Fragment (see your imports), then you need to use getSupportFragmentManager() instead

    Hope it helps

    0 讨论(0)
  • 2020-12-01 16:18

    you just have to import android.support.v4.app.Fragment; in the all the FragmentClass();. that's it.

    0 讨论(0)
提交回复
热议问题