gpt4 book ai didi

android - 无法从 Fragment 转换到 SupportMapFragment

转载 作者:搜寻专家 更新时间:2023-11-01 08:48:15 24 4
gpt4 key购买 nike

public class OutdoorFragment extends Fragment  {
private GoogleMap googleMap;
double latitude = 1.31039;
double longitude = 103.7784;

public OutdoorFragment() {
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_outdoor, container,
false);

try {
// Loading map
initilizeMap();

} catch (Exception e) {
e.printStackTrace();
}

googleMap.getUiSettings().setZoomGesturesEnabled(true);

googleMap.getUiSettings().setRotateGesturesEnabled(true);

googleMap.getUiSettings().setMyLocationButtonEnabled(true);

googleMap.setMyLocationEnabled(true);

// create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Koh Chi Hao ");

// adding marker

marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
googleMap.addMarker(marker);


return rootView;
}


private void initilizeMap() {
if (googleMap == null) {
googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity().getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}

@Override
public void onResume() {
super.onResume();
initilizeMap();
}

你好,在我的 initializeMap() 中,googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();一直说 Cannot cast from Fragment to SupportMapFragment 。我不确定发生了什么。提前致谢

最佳答案

改变;

googleMap = ((SupportMapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();

有了这个;

googleMap = ((SupportMapFragment) getSupportFragmentManager().
findFragmentById(R.id.map)).getMap();

请注意,getSupportFragmentManager 来自 android.support.v4.app.FragmentActivity

祝你好运。

关于android - 无法从 Fragment 转换到 SupportMapFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26137842/

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