gpt4 book ai didi

java - Android Studio "error: incompatible types: Fragment cannot be converted to SupportMapFragment"

转载 作者:行者123 更新时间:2023-12-01 19:47:12 26 4
gpt4 key购买 nike

您好,我正在尝试在 fragment 中创建 map ,我按照在线教程进行操作,该教程旨在将其放入 Activity 中,但我遇到了问题。我到处都找过了,但找不到解决方案。

当我尝试在 android studio 中编译时,它在我的 map 中出现错误“初始化行错误:

 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; (我必须由于其他应用程序错误而保留)

这是我的 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;
}

}

最佳答案

This problem occured after i changed my import from import android.support.v4.app.Fragment; to import android.app.Fragment; (which i have to keep due to other application errors)

如果不使用支持库 Fragment API,您无法使用SupportMapFragmentSupportMapFragment 一个支持库Fragment,虽然框架Fragment和支持库Fragment具有相同的名称和非常相似的方法,但它们不是相同且不能混合。

SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); //ignore warning

使用框架 Fragment API(而不是支持库 API)时,getChildFragmentManager() 只能管理框架 Fragment。因此,此 FragmentManager 不可能返回 SupportMapFragment。

如果您想使用SupportMapFragment,则必须更改应用程序的其余部分以允许您使用支持库Fragments。

关于java - Android Studio "error: incompatible types: Fragment cannot be converted to SupportMapFragment",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52742836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com