javaandroidandroid-studioandroid-fragmentscradle

Incompatible types cannot be converted to Fragment


So i'm trying to make a simple app with bottom navigation, but i'm kinda stuck with the Fragment things.

I these error:

error: incompatible types: berandaFragment cannot be converted to Fragment  
error: incompatible types: beritaFragment cannot be converted to Fragment   
error: incompatible types: radioFragment cannot be converted to Fragment    
error: incompatible types: kegiatanFragment cannot be converted to Fragment 

This is my MainActivity.java

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView mMenuUtama;
    private FrameLayout mFrameUtama;

    private berandaFragment berandaFragmentMenu;
    private beritaFragment beritaFragmentMenu;
    private radioFragment radioFragmentMenu;
    private kegiatanFragment kegiatanFragmentMenu;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFrameUtama = (FrameLayout) findViewById(R.id.frame_utama);
        mMenuUtama = (BottomNavigationView) findViewById((R.id.menu));

        berandaFragmentMenu = new berandaFragment();
        beritaFragmentMenu = new beritaFragment();
        radioFragmentMenu = new radioFragment();
        kegiatanFragmentMenu = new kegiatanFragment();

        mMenuUtama.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.menu_beranda :
                            setFragment(berandaFragmentMenu);
                            return true;

                        case R.id.menu_berita :
                            setFragment(beritaFragmentMenu);
                            return true;

                        case R.id.menu_radio :
                            setFragment(radioFragmentMenu);
                            return true;

                        case R.id.menu_kegiatan :
                            setFragment(kegiatanFragmentMenu);
                            return true;

                        default:
                            return false;
                }
            }
        });
    }

    public void setFragment(Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.frame_utama, fragment);
        fragmentTransaction.commit();
    };}

I'm a beginner, sorry if the solution is found to be very simple. I actually followed a tutorial to make this, and i followed every step, but i got those errors, so i'm very confused.

Thank you before.


Solution

  • This might be due to the fact that you imported the app version of fragment instead of the framework or v4 version. You have defined your Activity to extend AppCompat which is part of the support framework. Owing to this, all fragments hosted by this Activity must from the support framework ,ie of the android.support.v4.app.Fragment. You can either make your activity extend simple Activity or convert your fragment to the v4 type. This decision would depend on the level of devices you are aiming to target.

    This article talks in much detail about the support framework:

    http://martiancraft.com/blog/2015/06/android-support-library/