2

I was following the directions from this post on how to switch between fragments within the Sliding Drawer Activity in Android Studio and I'm positive I've followed the steps correctly as displayed. However, at the end of my displayView method the compiler is complaining that a fragment object I am referencing is incompatible with the method I'm passing it into. The method requires android.support.v4.app.Fragment whereas the fragment object I am using is (apparently) android.app.Fragment. Although I know this to be false as well because if I explicitly define my Fragment like so :

android.support.v4.app.Fragment fragment = null;

the original error is fixed but rest of the references throughout the switch case begin complaining for the same reason:

Error:(135, 44) error: incompatible types: android.app.Fragment cannot be converted to android.support.v4.app.Fragment

I have absolutely no idea what could be causing such an issue, however I'm no expert at using the Android SDK; there could very well be something I'm just missing. All feedback and assistance is appreciated; I'm on a time crunch for this app and need to finish it very soon. Thank you!

Below is the displayView method:

public void displayView(int viewId) {

    Fragment fragment = null;
    String title = getString(R.string.app_name);

    switch (viewId) {
        case R.id.nav_newsfeed:
            fragment = new Feed_Fragment();
            title  = "News Feed";

            break;
        case R.id.nav_camera:
            fragment = new Post_Fragment();
            title = "Post a Picture";
            break;
        case R.id.nav_gallery:
            fragment = new Gallery_Fragment();
            title = "Your Posts";

            break;
        case R.id.nav_send:
            fragment = new Message_Fragment();
            title = "Direct Message";

            break;
        /*case R.id.nav_manage:
            //fragment = new Settings_Fragment();
            title = "Account Settings";

            break;
        case R.id.nav_share:
            //fragment = new Share_Fragment();
            title = "Share";

            break;*/

    }
    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment); //2nd parameter causes issue
        ft.commit();
    }

    // set the toolbar title
    if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle(title);
    }

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

}
Community
  • 1
  • 1
McFizz
  • 3,013
  • 3
  • 18
  • 26
  • 2
    Just change your Fragments class to extend from `android.support.v4.app.Fragment` instead of `android.app.Fragment`. – Rami Jan 13 '16 at 09:17

3 Answers3

4

Error:(135, 44) error: incompatible types: android.app.Fragment cannot be converted to android.support.v4.app.Fragment

You are using getSupportFragmentManager

Just import android.support.v4.app.Fragment instead of instead of android.app.Fragment

You should be doing

import android.app.Fragment

Change that to

import android.support.v4.app.Fragment

Then Clean-Rebuild your Project .

Try this .Hope this helps.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    Thanks! My problem was that I forgot to import the proper `Fragment` class in the fragments I was using within my `switch` `case`. Thanks for the help! – McFizz Jan 13 '16 at 14:49
1

welcome to polymorphism through Inheritance

try to understand although you are trying to change Fragment which is here android.support.v4.app.Fragment your reference variable should also be from that class, right now your reference variable fragment is of type is here android.app.Fragment. as your method does not get the reference variable of type android.support.v4.app.Fragment, it shows you the error

as there is conflict in it you have to change the Fragment fragment to get the the class from here android.support.v4.app.Fragment

When your are dealing with fragments, make sure your from where are you referencing them from. As this is the common problem. Many confuse between android.app.Fragment to android.support.v4.app.Fragment

what you need is android.support.v4.app.Fragment for the reference variable for type Fragment

Pankaj Nimgade
  • 4,529
  • 3
  • 20
  • 30
1

Your Fragments (Feed_Fragment, Post_Fragment, Gallery_Fragment, Message_Fragment...) must extend from android.support.v4.app.Fragment instead of android.app.Fragment.

Also in your activity import android.support.v4.app.Fragment instead of android.app.Fragment.

Rami
  • 7,879
  • 12
  • 36
  • 66