error: incompatible types: MapsFragment can not be converted to Fragment

0

I'm trying to add the Google Maps API snippet to the main project screen

error: Incompatible types: MapsFragment can not be converted to Fragment

part of the code:

    fragmentManager = getSupportFragmentManager();

    FragmentTransaction transaction = fragmentManager.beginTransaction();

    transaction.add(R.id.container, new MapsFragment(), "MapsFragment" ) ;

    transaction.commitAllowingStateLoss();

The error appears when I give the New .. command that it can not be converted to a fragment ..

How do I use it on the main screen then?

    
asked by anonymous 03.07.2018 / 16:25

1 answer

0

Eae, I just came across this problem, the solution was to add an import

import android.support.v4.app.Fragment;

instead of import android.app.Fragment; In the case of the class Fragment that will call.

After this I still had another error, where the application closes when clicking on the event that would lead me to the fragment, so I think it may have happened to you. the solution is: We need to call and implement the methods of this interface OnFragmentInteractionListener for each class that will be called as a fragment.

 public class MainActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener, PrincipalFragment.OnFragmentInteractionListener ,QuemSomos.OnFragmentInteractionListener {

In my case I added MainActivity and I used alt + enter to generate. I hope I have helped after that everything worked perfectly. I do not forget to mark the best answer if this answer was useful for you.

    
12.09.2018 / 04:25