gpt4 book ai didi

android - 从 fragment 到 SupportMapFragment 的不可转换类型错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:33:06 26 4
gpt4 key购买 nike

所以我有一个 fragment 类:

public class MyMapFragmentActivity extends Fragment implements LocationListener,
GoogleMap.OnMapClickListener, GoogleMap.OnMapLongClickListener {

我在其中定义了一个 SupportMapFragment:

private SupportMapFragment fragment;

我正在尝试使用 findFragmentById:

    @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (fragment == null) {
fragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.content_frame, fragment).commit();
}
}

线

fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);

尽管“fragment ”是 SupportMapFragment,但它给出了不可转换的类型错误: 无法将 android.app.fragment 转换为 com.google.android.gms.maps.SupportMapFragment!

我正在使用 fragment 布局:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />

最佳答案

如果您使用兼容性库 - 使用 getSupportFragmentManager().findFragmentById(R.id.fragment); 否则从 android.app.Fragment 而不是 扩展android.support.v4.app.fragment.

关于android - 从 fragment 到 SupportMapFragment 的不可转换类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27588424/

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