JPJens
JPJens

Reputation: 1205

Android navigation drawer fragment - incompatible types

In android studio I am attempting to open a new fragment from within my navigation drawer, using the method found here

Unfortunately i get an error with the below method:

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    Fragment fragment;
    FragmentManager fragmentManager = getSupportFragmentManager();
    if(position==1) {
        fragment = new BlankFragment(); // This is where it fails with incompatible types
        fragmentManager.beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
    }else{

        fragment = PlaceholderFragment.newInstance(position +1);
        fragmentManager.beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
    }
}

As for imports I am using the support type:

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

My BlankFragment class extends Fragment:

public class BlankFragment extends Fragment { ... }

The problem is that the line: fragment = new BlankFragment(); is failing with the message:

"Incompatible types"

Upvotes: 0

Views: 319

Answers (1)

Benjamin Scharbau
Benjamin Scharbau

Reputation: 2089

Are you importing android.support.v4.app.Fragment in BlankFragment?

Otherwise you are trying to add a regular Fragment to the SupportFragmentManager. I did this mistake many times, and it was hard to find in the beginning.

Upvotes: 2

Related Questions