Inconvertible types; cannot cast android.app.fragment
Your opcion1Fragment
is extending from android.app.Fragment;
. But you need to use class from android.support.v4.app
. So you need to:
- Change your import from:
import android.app.Fragment;
toimport android.support.v4.app.Fragment;
- Change your import from
import android.app.FragmentManager;
toimport android.support.v4.app.FragmentManager;
And your problem will be fixed.
Hope this helps!!
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
I am a newbie to programming in android studio, I am trying to work with Google Maps (setting up destination, routes and more) in a navigation drawer, where I have multiple fragments, and one specifically for Google maps, I was Having trouble to use markers and work on the map with the
onMapReady()
method, I was able to find that instead i should use theonActivityCreated()
method, but once i started working on it, it started giving me the following error: Inconvertible types, cannot castandroid.app.Fragment
tocom.google.android.gms.maps.SupportMapFragment
. in the following line of code:mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
as well gives me a second error: Wrong 2nd argument type, Found:com.google.android.gms.maps.SupportMapFragment
Required:android.app.Fragment
in the following line of code:fm.beginTransaction().replace(R.id.map, mapFragment).commit();
. Unfortunately i havent been able to find an answer or solution to this problem and will appreciate your help.Here is my the complete code for the fragment where the map is located:
package demo.mapas; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; //import android.support.v4.app.FragmentManager; import android.app.FragmentManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.usuario.mapas.R; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; /** * Created by USUARIO on 3/12/2016. */ public class opcion1Fragment extends Fragment { private SupportMapFragment mapFragment; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_option1, container, false); return rootView; } @Override public void onActivityCreated(Bundle savedInstanceState){ super.onActivityCreated(savedInstanceState); FragmentManager fm = getChildFragmentManager(); mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map); if(mapFragment == null){ mapFragment = SupportMapFragment.newInstance(); fm.beginTransaction().replace(R.id.map, mapFragment).commit(); } else { mapFragment.getMapAsync(new OnMapReadyCallback() { @Override public void onMapReady(GoogleMap googleMap) { googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); } }); } } }