Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: incompatible types: Fragment cannot be converted to MapFragment

In my project I'm using a navigation drawer with tabs. In one of the tabs i want to call a google map but them i face this error:

Error: incompatible types: Fragment cannot be converted to MapFragment

The Fragment which contains the map:

public class PrimaryFragment extends Fragment implements OnMapReadyCallback{

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.primary_layout,container,false);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        //toDO 
    }
}

The Tab Fragment

public class TabFragment extends Fragment {

public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View x =  inflater.inflate(R.layout.tab_layout,null);
        tabLayout = (TabLayout) x.findViewById(R.id.tabs);
        viewPager = (ViewPager) x.findViewById(R.id.viewpager);
    viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

    tabLayout.post(new Runnable() {
        @Override
        public void run() {
                tabLayout.setupWithViewPager(viewPager);
               }
    });

    return x;

}

class MyAdapter extends FragmentPagerAdapter{

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position)
    {
      switch (position){
          case 0 : return new PrimaryFragment();
          case 1 : return new SocialFragment();
          case 2 : return new UpdatesFragment();
      }
    return null;
    }

    @Override
    public int getCount() {

        return int_items;

    }

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position){
            case 0 :
                return "Primary";
            case 1 :
                return "Social";
            case 2 :
                return "Updates";
        }
            return null;
    }
}
}

The Main Activity

public class MainActivity extends FragmentActivity{
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
         mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
         mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
         mFragmentManager = getSupportFragmentManager();
         mFragmentTransaction = mFragmentManager.beginTransaction();
         mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();

         mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
         @Override
         public boolean onNavigationItemSelected(MenuItem menuItem) {
            mDrawerLayout.closeDrawers();



             if (menuItem.getItemId() == R.id.nav_item_sent) {
                 FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                 fragmentTransaction.replace(R.id.containerView,new SentFragment()).commit();

             }

            if (menuItem.getItemId() == R.id.nav_item_inbox) {
                FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                xfragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
            }

             return false;
        }

    });

            android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
            ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
            R.string.app_name);

            mDrawerLayout.setDrawerListener(mDrawerToggle);

            mDrawerToggle.syncState();
}
}

The xml of primary_fragment

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
    class="com.google.android.gms.maps.MapFragment"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.google.android.gms.maps.MapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

I'm importing android.support.v4.app.Fragment on the fragments and android.support.v4.app.FragmentActivity on the main activity.

like image 844
Artur Galdino Avatar asked Mar 09 '16 19:03

Artur Galdino


3 Answers

Using with SupportMapFragment solved on my side : (tnx @Itzzik Samara)

SupportMapFragment mMapFragment = SupportMapFragment.newInstance();

instead of

MapFragment mMapFragment = MapFragment.newInstance();
like image 111
ergunkocak Avatar answered Nov 18 '22 14:11

ergunkocak


This seems to be a casting issue to me you are trying to cast to a MapFragment

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
like image 25
Sajal Choukse Avatar answered Nov 18 '22 14:11

Sajal Choukse


  1. Go to your MainActivity.java
  2. Remove this line of code: SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager() .findFragmentById(R.id.map);
  3. Now replace the Point No 2 code by this new code: MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
  4. Now rebuild your project. You are ready to go.
like image 40
Ajay Avatar answered Nov 18 '22 15:11

Ajay