我有一些使用FragmentPagerAdapter
创建的标签,并且使用此适配器我使用getItem
方法获取“标签碎片”:
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new MapsFragment();
case 1:
return new GroupsFragment();
case 2:
return new InfosFragment();
}
return null;
}
现在我想在我的1. Tab上实现一个MapFragment我已经有了这个:
public class MapsFragment extends Fragment implements OnMapReadyCallback {
private MapFragment mapFragment;
public static MapsFragment newInstance() {
MapsFragment fragment = new MapsFragment();
return fragment;
}
public MapsFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_maps, container, false);
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng augsburg = new LatLng(48.348527, 10.915952);
googleMap.setMyLocationEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(augsburg, 13));
googleMap.addMarker(new MarkerOptions()
.title("Augsburg Zoo")
.snippet("Der coolste Zoo der Welt")
.position(augsburg));
}
}
但是我在这一行上遇到了一个不兼容的类型错误:
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
但我不知道如何修复它以及如何编写比这更好的代码!
答案 0 :(得分:1)
您可能正在使用SupportMapFragment。
将onCreate()更改为
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_maps, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return view;
}