error: incompatible types: Mapsfragment cannot be converted to Fragment

Asked

Viewed 204 times

0

I am trying to add the Google Maps API fragment to the main project screen

error: incompatible types: Mapsfragment cannot 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 says that it cannot be converted to fragment..

how do I use it on the main screen then ?

1 answer

0

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

import android.support.v4.app.Fragment;

in place of import android.app.Fragment; In this case, the Fragment class you will call.

After that I still thought of another error, where the application closes when clicking on the event that would take me to 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 Fragment.

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.