Good morning, I'm doing the exercise of placing a map to my app, I already did an exercise that worked correctly for me using an activity, but now I'm doing it with Fragment
in a navigation drawer and it shows me error in the line:
Shown: Inconvertible types; cannont cast 'android.support.v4.app.Fragment' to 'com.google.android.gms.maps.MapFragment'
MapFragment fragment =(MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
If someone can help me, thank you very much.
Next the code:
public class MapActivity extends Fragment implements OnMapReadyCallback {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.map_layout, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng bogota = new LatLng(4.653421, -74.145150);
googleMap.addMarker(new MarkerOptions().position(bogota)
.title("Uniagustiniana"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(bogota, 16.1f));
}
}
The file map_layout.xml
:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tecnologias.uniagustinianaapp.MapActivity">
<fragment android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"></fragment>
As it is a set of fragments that are replaced by transactions in MainActivity, when I change // import android.support.v4.app.Fragment; a // import android.app.Fragment; I get error in the MainActivity this because in all the fragments I import support.v4, the following is the MainActivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, Portal.OnFragmentInteractionListener, Biblioteca.OnFragmentInteractionListener, Evu.OnFragmentInteractionListener,
Directorio.OnFragmentInteractionListener, Siga.OnFragmentInteractionListener, Moodle.OnFragmentInteractionListener, Facebook.OnFragmentInteractionListener,
Twitter.OnFragmentInteractionLiastener, LinkedIn.OnFragmentInteractionListener, GooglePlus.OnFragmentInteractionListener, Flickr.OnFragmentInteractionListener,
Tour.OnFragmentInteractionListener, Instagram.OnFragmentInteractionListener, Youtube.OnFragmentInteractionListener, Noticias.OnFragmentInteractionListener,
Home.OnFragmentInteractionListener, Whatsapp.OnFragmentInteractionListener, MapActivity.OnFragmentInteractionListener {
//Bottom
private BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);//Los iconos del menu lateral toman su color original
//BOTTON NAVIGATION VIEW
final Fragment home = new Home();
final Fragment noticias = new Noticias();
final Fragment calendario = new Calendario();
final Fragment ubicacion = new MapActivity();
final Fragment pqrs = new PQRS();
final Fragment preinscrip = new PreInscripcion();
//FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
//fragmentTransaction.replace(R.id.content_main, home).commit();
//FragmentManager fragmentManager = getSupportFragmentManager();
//fragmentManager.beginTransaction().replace(R.id.content_main, new Home()).commit();
if(savedInstanceState == null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_main, new Home()).commit();
}
bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
bottomNavigationView.setItemIconTintList(null);//Los iconos del menu Bottom toman su color original
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
if (item.getItemId() == R.id.noticias) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_main, noticias).commit();
} else if (item.getItemId() == R.id.rutas) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_main, ubicacion).commit();
} else if (item.getItemId() == R.id.cal_aca) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_main, calendario).commit();
} else if (item.getItemId() == R.id.pqrs) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_main, pqrs).commit();
} else if (item.getItemId() == R.id.preinscripcion) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_main, preinscrip).commit();
}
return true;
}
});
}