Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconvertible types; cannot cast android.app.fragment

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 the onActivityCreated() method, but once i started working on it, it started giving me the following error: Inconvertible types, cannot cast android.app.Fragment to com.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);

                }
            });
        }
    }

}
like image 598
Ariel Guevara Avatar asked Dec 18 '22 18:12

Ariel Guevara


1 Answers

Your opcion1Fragment is extending from android.app.Fragment;. But you need to use class from android.support.v4.app. So you need to:

  1. Change your import from: import android.app.Fragment; to import android.support.v4.app.Fragment;
  2. Change your import from import android.app.FragmentManager; to import android.support.v4.app.FragmentManager;

And your problem will be fixed.

Hope this helps!!

like image 138
Gueorgui Obregon Avatar answered Dec 21 '22 11:12

Gueorgui Obregon