public class PlaceMapsFragment extends SupportMapFragment {
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View v = inflater.inflate(R.layout.fragment_place_maps, container,
false);
setUpMapIfNeeded();
return v;
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((MapView) findViewById(R.id.map)).getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title(
"Marker"));
}
}
Error
The method findViewById(int) is undefined for the type PlaceMapsFragment
If I remember correctly, All Fragments do not have a native
findViewById()method. This is why you have to use the onCreateView() method.This means that you should change
setUpMapIfNeeded()tosetUpMapIfNeeded(View view)and inonCreateView()pass off v to your modified method. You also need to changemMap = ((MapView) findViewById(R.id.map)).getMap();tomMap = ((MapView) view.findViewById(R.id.map)).getMap();.