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);
}