Próbuję dynamicznie utworzyć SupportMapFragment
i umieścić go w kontenerze FrameLayout
.Funkcja getMap() funkcji SupportMapFragment zwraca wartość null
Mój problem jest to, że mMapFragment.getMap()
powraca null
...
ktoś może pomóc?
CenterMapFragment.java
public class CenterMapFragment extends Fragment {
private SupportMapFragment mMapFragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.center_map_fragment, container, false);
}
@Override
public void onActivityCreated (Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()) == ConnectionResult.SUCCESS) {
setUpMapIfNeeded();
}
}
private void setUpMapIfNeeded() {
if (mMapFragment == null) {
mMapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction =
getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.map, mMapFragment);
fragmentTransaction.commit();
setUpMap();
}
}
private void setUpMap() {
GoogleMap map = mMapFragment.getMap();
// map is null!
}
@Override
public void onResume()
{
super.onResume();
setUpMapIfNeeded();
}
}
center_map_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<Button
android:id="@+id/btn_loc"
android:layout_width="60dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/locbtn" />
</RelativeLayout>
Jeśli 'CenterMapFragment' rozszerza' SupportMapFragment', jak mogę ustawić mapę na 'FrameLayout'? Muszę zmienić układ? – jul
@jul: W jednym podejściu 'CenterMapFragment' zaimplementował' onCreateView() ', który najpierw wywoływał' super.onCreateView() ', aby uzyskać wszystko, co' SupportMapFragment' tworzy. Następnie utworzyłby 'FrameLayout' i dodał utworzony' super' widok 'View' jako element potomny' FrameLayout'. Wtedy zwróciłby 'FrameLayout' z' onCreateView() '. To opakowuje zawartość 'SupportMapFragment' w dowolnym' 'CenterMapFragment" chce. Albo "CenterMapFragment" jest zwykłym 'Fragmentem' i użyj' MapView' zamiast 'SupportMapFragment'. Lub znajdź lepszy czas na wywołanie 'setUpMap()' z twoim aktualnym kodem. – CommonsWare
Ok, użyję 'MapView'. – jul