gpt4 book ai didi

android - 如何在 Fragment 中使用 SupportMapFragment?

转载 作者:IT老高 更新时间:2023-10-28 23:26:28 26 4
gpt4 key购买 nike

我知道使用嵌套 fragment 存在问题。但是我的应用程序被设计为在 fragment 上运行,如果我将使用 map Activity ,我的转换功能将会出错。

我想就如何实现这一目标向您寻求帮助。我一直在互联网上搜索,但找不到最佳解决方案。

我试过这段代码:

private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) myFragmentActivity.getSupportFragmentManager().findFragmentById(R.id.map_con))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setMyLocationEnabled(true);
}
}
}

这会给我重复的错误,因为 R.id.map_con 是我的 fragment 中的 fragment 。

所以我寻找解决方法,这一次,R.id.map_con 是一个框架布局,在运行时我为它创建了 SupportMapFragment。

SupportMapFragment mSupportMapFragment = new SupportMapFragment();
myFragmentActivity.getSupportFragmentManager().beginTransaction()
.replace(R.id.map_con, mSupportMapFragment).commit();

虽然这不会在每次关闭和打开 fragment 时给我一个副本。但我的错误是 mSupportMapFragment.getMap 始终为空。我不明白为什么它为空?。

mMap = mSupportMapFragment.newInstance().getMap();
if (mMap != null){
Log.e("ReportFragment","mMap is not empty");
}else{
Log.e("ReportFragment","mMap is empty");
}

非常感谢你们提供的任何意见,或者您是否还有其他解决方法但仍在此过程中,即 fragment 内的 fragment

谢谢

chkm8

最佳答案

getMap() 已弃用

Fragment 中的代码应该是这样的:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.fragment_location, container, false);

mMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mMapFragment.getMapAsync(this);

return v;
}

关于android - 如何在 Fragment 中使用 SupportMapFragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25051246/

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