Java 安卓工作室“;错误:不兼容的类型:无法将片段转换为SupportMapFragment;

Java 安卓工作室“;错误:不兼容的类型:无法将片段转换为SupportMapFragment;,java,android,google-maps,android-fragments,Java,Android,Google Maps,Android Fragments,嗨,我试图在一个片段中创建一个地图,我遵循了一个在线教程,该教程的目的是让它出现在一个活动中,我遇到了一个问题。 我到处找了,找不到解决办法 当我尝试在android studio中编译时,它在我的映射中给了我一个错误“初始化行错误: incompatible types: Fragment cannot be converted to SupportMapFragment" 错误发生在initMap()上: 行: SupportMapFragment mapFragment = (Supp

嗨,我试图在一个片段中创建一个地图,我遵循了一个在线教程,该教程的目的是让它出现在一个活动中,我遇到了一个问题。 我到处找了,找不到解决办法

当我尝试在android studio中编译时,它在我的映射中给了我一个错误“初始化行错误:

 incompatible types: Fragment cannot be converted to SupportMapFragment"
错误发生在
initMap()
上:

行:

SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
此问题发生在我将导入内容从
import android.support.v4.app.Fragment;
更改为
import android.app.Fragment;
(由于其他应用程序错误,我必须保留该内容)之后

这是我的片段代码:

public class MapFragment extends Fragment implements OnMapReadyCallback
{

    private static final String TAG = "MapFragment";

    private Boolean LocationPermissionsGranted = false;  //boolean for location method
    private GoogleMap Map;
    private static final int ERROR_REQUEST = 69; //error code relates to isservicesOK method
    private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
    private static final String COURSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
    private static final int LOCATION_PERMISSION_CODE = 1234;

    public MapFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        //TODO: Instead of hardcoding the title perhaps take the user name from somewhere?
        // Note the use of getActivity() to reference the Activity holding this fragment
        getActivity().setTitle( "YOUR CLINICS" );
        isServicesOK();
        getLocationPermission();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        ButterKnife.bind( getActivity() );

        return inflater.inflate( R.layout.fragment_map, container, false );


    }
    @Override
    public void onMapReady(GoogleMap googleMap) {
        Toast.makeText(getActivity(), "Maps is ready", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "Map is ready");
        Map = googleMap;
    }
    private void initMap() {
        Log.d(TAG, "initializing map...");
        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); //ignore warning

        mapFragment.getMapAsync(MapFragment.this);
    }

    private void getLocationPermission()
    {
        Log.d(TAG, "Retrieving location permissions");
        String[] permissions = {android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.ACCESS_COARSE_LOCATION};

        if (ContextCompat.checkSelfPermission(this.getActivity().getApplicationContext(), //ignore
                FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            if (ContextCompat.checkSelfPermission(this.getActivity().getApplicationContext(),
                    COURSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                LocationPermissionsGranted = true;
            } else {
               requestPermissions(permissions,
                        LOCATION_PERMISSION_CODE);
            }
        } else {
            requestPermissions(permissions,
                    LOCATION_PERMISSION_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
    {
        Log.d(TAG, "onRequestPermissionsResult: called.");
        LocationPermissionsGranted = false;

        switch (requestCode)
        {
            case LOCATION_PERMISSION_CODE:
            {
                if (grantResults.length > 0)
                {
                    for (int i = 0; i < grantResults.length; i++) //ignore
                    {
                        if (grantResults[i] != PackageManager.PERMISSION_GRANTED)
                        {
                            LocationPermissionsGranted = false;
                            Log.d(TAG, "onRequestPermissionsResult: permission failed");
                            return;
                        }
                    }
                    Log.d(TAG, "onRequestPermissionsResult: permission granted");
                    LocationPermissionsGranted = true;
                    //if permissions are granted we initialize the map
                    initMap();
                }
            }
        }
    }


    // integrate this method with the start of the application eg after a button has been clicked
    // Debugging method- checks if google services is available/functional
    public boolean isServicesOK ()
    {
        Log.d(TAG, "isServicesOK: checking if your Google Services is up to date"); //debugging

        int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(getActivity()); /*//log d = debug*/

        if (available == ConnectionResult.SUCCESS)
        {
            /*//checks if everything is fine*/
            Log.d(TAG, "isServicesOK: Google Play Services is functional");
            return true;
        }
        else if (GoogleApiAvailability.getInstance().isUserResolvableError(available))
        {
            /* //If an error occurs, and it is resolvable*/
            Log.d(TAG, "isServicesOK: A fixable error has occured!");
            Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(getActivity(), available, ERROR_REQUEST);
            dialog.show();  /*//This prompts google to provide a error message and solution when a fixable error has occurred*/
        }

        else
        {
            Toast.makeText(getActivity(), "Google Play Services is dysfunctional, sorry!", Toast.LENGTH_SHORT).show(); /*//if it cant be fixed for whatever reason lmao*/
        }
        return false;
    }

}
公共类MapFragment扩展了MapReadyCallback上的片段实现
{
私有静态最终字符串TAG=“MapFragment”;
私有布尔LocationPermissionsGranted=false;//位置方法的布尔值
私人谷歌地图;
私有静态final int ERROR_REQUEST=69;//错误代码与isservicesOK方法有关
私有静态最终字符串FINE\u LOCATION=Manifest.permission.ACCESS\u FINE\u LOCATION;
私有静态最终字符串COURSE\u LOCATION=Manifest.permission.ACCESS\u rough\u LOCATION;
私有静态最终整数位置\权限\代码=1234;
公共映射片段(){
//必需的空公共构造函数
}
@凌驾
创建时的公共void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//TODO:与其硬编码标题,不如从某处获取用户名?
//注意使用getActivity()来引用包含此片段的活动
getActivity().setTitle(“您的诊所”);
isServicesOK();
getLocationPermission();
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
ButterKnife.bind(getActivity());
返回充气机。充气(R.layout.fragment_图,容器,假);
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
Toast.makeText(getActivity(),“映射已就绪”,Toast.LENGTH_SHORT.show();
Log.d(标记“地图准备就绪”);
地图=谷歌地图;
}
私有void initMap(){
Log.d(标记“初始化映射…”);
SupportMapFragment mapFragment=(SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);//忽略警告
getMapAsync(mapFragment.this);
}
私有void getLocationPermission()
{
Log.d(标记“检索位置权限”);
字符串[]权限={android.Manifest.permission.ACCESS\u FINE\u位置,
android.Manifest.permission.ACCESS\u\u LOCATION};
if(ContextCompat.checkSelfPermission)(this.getActivity().getApplicationContext(),//忽略
精细位置)==PackageManager.PERMISSION(已授予权限){
如果(ContextCompat.checkSelfPermission)(this.getActivity().getApplicationContext(),
课程位置)==软件包管理员权限(已授予){
LocationPermissionsGranted=真;
}否则{
请求权限(权限,
位置(许可)(代码);;
}
}否则{
请求权限(权限,
位置(许可)(代码);;
}
}
@凌驾
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults)
{
d(标记“onRequestPermissionsResult:called”);
LocationPermissionsGranted=错误;
开关(请求代码)
{
案例位置\许可\代码:
{
如果(grantResults.length>0)
{
for(int i=0;iSupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); //ignore warning