mapa android we fragmencie

Próbuję utworzyć aplikację dla systemu Android, która ma jedną aktywność i wiele fragmentów. Każdy fragment zajmie cały ekran, gdy jest widoczny, a przy transakcji wymiany powinien zostać przełączony na inny fragment.

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

To jest pojemnik na fragmenty. Problem polega na tym, że fragment, którego chcę użyć, to GoogleMap. To jest mój kod fragmentu mapy:

public class Map extends Fragment{
private final static String TAG = "MAP Fragment";
public GoogleMap googleMap = null;

@Override
public void onCreate(Bundle savedInstanceState) 
{
        super.onCreate(savedInstanceState);
        setUpMapIfNeeded();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.map_fragment, null);
    return view;
}

private void setUpMapIfNeeded()
{
    if(googleMap == null)
    {
        googleMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();
        if(googleMap != null)
        {
            GoogleMapOptions options = new GoogleMapOptions();
            options.mapType(GoogleMap.MAP_TYPE_NORMAL)
                .camera(new CameraPosition(new LatLng(25f, 47f), 13f, 0f, 0f));
        }
        else
        {
            Log.d(TAG, "googleMap is null !!!!!!!!!!!!!!!");
        }
    }
}

}

i jego układ:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:name="com.google.android.gms.maps.MapFragment"/>

A w głównej działalności:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
Map test = new Map();
getSupportFragmentManager().beginTransaction()
            .add(R.id.content_frame, test)
            .commit();
}

Mam wyjątek zerowego wskaźnika w: (wierzę, że nie może znaleźć R.id.map)

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

Jeśli nie użyję „getActivity ()”. mówi, że nie może rzutować z Fragmentu na MapFragment.

Potrzebuję „googleMap” do tworzenia znaczników na mapie.

Kiedy tworzę tę samą aplikację, ale bez fragmentów (moja główna aktywność zanurza mapę i nie mam innych fragmentów), wszystko działa dobrze.

Znalazłem podobne tematy:Google Maps API v2 Custom MapFragment + SimpleFragment ibłąd podczas używania map we fragmencie . Ale jeśli rozszerzę FragmentActivity lub SupportMapFragment, nie mogę zastąpić fragmentu innym przy użyciu transakcji.

Wniosek: Chciałbym, aby w momencie wystąpienia akcji (kliknięcie lub coś) musiałem zastąpić fragmensa w ten sposób:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);

Co ja robię źle? Czy powinienem przyjąć inne podejście do problemu?

(P.S. Naprawdę jestem nowy na Androida)

questionAnswers(1)

yourAnswerToTheQuestion