I'd like to build a Fragment
which consists of a full screen map overlaid with other views (A ViewPager
, EditText
, and the like). I've thought of two different options:
- Create a
Fragment
with aMapView
near the root of the layout and set it tomatch_parent
, then use otherView
's as normal. - Subclass
MapFragment
.
I've subclassed MapFragment
:
public class ListMapFragment extends MapFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
}
Displaying this fragment works as normal and the map takes up the entire fragment. However, I would like to display various views as described above over the map. If I try to inflate and return a view (as you would with a normal Fragment
) by instead returning a view, a NPE occurs:
public class ListMapFragment extends MapFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list_map, container, false);
return view;
}
}
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void maps.ad.y.v()' on a null object reference
If I try calling the supers method before returning a view, the layout is loaded, but the map is not:
public class ListMapFragment extends MapFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list_map, container, false);
super.onCreateView(inflater, container, savedInstanceState);
return view;
}
Out of the two options, is one preferred over the other? And if #2 is preferred, what is wrong with the above code?