您好,我试图在一个片段中创建地图,我按照一个在线教程进行操作,该教程旨在使它处于活动状态并且我遇到问题。我到处都看过,找不到解决方案。
当我尝试在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;
}
}
我从导入android.support.v4.app.Fragment更改导入后发生了此问题;导入android.app.Fragment;
(由于其他应用程序错误,我必须保留)
如果 不 使用SupportMapFragment
支持库Fragment API, 就无法
使用。SupportMapFragment
是
一个支持库Fragment,尽管框架Fragment和支持库Fragment具有相同的名称和非常相似的方法,但它们 并不 相同且不能混用。
SupportMapFragment mapFragment = (SupportMapFragment)
getChildFragmentManager().findFragmentById(R.id.map); //ignore warning
使用框架片段API(而不是支持库API)时,getChildFragmentManager()
只能管理框架片段。因此,此FragmentManager不可能返回SupportMapFragment。
如果要使用SupportMapFragment,则必须更改应用程序的其余部分,以允许您使用支持库Fragments。
我正在尝试创建一个基本的谷歌地图android应用程序。就在我创建项目并获得API密钥之后,我就收到了这个错误。 “错误:不兼容类型:片段无法转换为SupportMapFragment” 行:SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager().findFragmentByID(r.id.map
问题内容: 错误使我发疯。 在我的应用中,我在MainActivity中创建了3个导航抽屉项,和。现在,我想添加两个与的项目。 MainActivity //用于导航抽屉 TabsFragmentPagerAdapter.java ViewView.java 问题: 错误:(49,81)错误:不兼容的类型:android.app.FragmentManager无法转换为android.suppor
我试图创建一个动态表单布局,当我构建我的项目时,我在代码的第37行得到以下错误“错误:不兼容的类型:NewSubscription不能转换为上下文”。是不是因为这是片段,不是Java课?如何解决此错误? NewSubscription.java fragment_new_subscription.xml
而当运行时,我收到的是: 错误:不兼容类型:int无法转换为客户端ClientDAO.Delete(clientId);
我试着在这个网站上搜索类似的问题,但没有找到任何地方他们试图使用一个int数来填充的方法的使用。
我是新到Java,我需要一个程序,接受4个整数输入,4个双输入,和3个字符输入。我准备好了整数和双倍输入。我真的需要帮助得到字符输入,请帮助。 我一直得到这个错误: 不兼容类型:char无法转换为char[]charValue=input.charat(0); 这是我的代码: