How to implement GoogleMap in a Fragment?
I'm trying to develop the same thing, but getMap() cannot be executed because SupportMapFragment (when executing the line (SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map) ) returns a null object.
I've tried extending MapFragment and SupportMapFragment instead of Fragment, but they didn't work...
I just don't want to create a new activity to show a map: I want to show a mere Map instead my main activity. Can I carry out my goal?
I have a main activity which extends ActionBarActivity, who replaces fragments inside a container (depending on the user selection on the drawer, X or Y fragment is replaced and shown). I own 2 fragments working, but I cannot accomplish the goal of manipulating a GoogleMap object inside a Fragment.
That's my mapa.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mapaPrincipal"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
That's my FragmentMapa class:
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.ingartek.bizkaimove.R;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentMapa extends Fragment {
private GoogleMap mMapa;
public FragmentMapa() {
// TODO Auto-generated constructor stub
}
@Override
public void onAttach (Activity activity) {
super.onAttach(activity);
}
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.mapa, container, false);
mMapa = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapaPrincipal)).getMap();
return rootView;
}
The line highlighted is not working, because the SupportMapFragment object is null and therefore, getMap() crashes.
Any help please? Thanks in advance.
SOLUTION TO MY PROBLEM
For anyone who faces a problem like mine: you shouldn't use getSupportFragmentManager(), instead you should use getChildFragmentManager(). This should remain that way:
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.mapa, container, false);
setupMapIfNeeded();
return rootView;
}
private void setupMapIfNeeded() {
if( mMapa == null ){
SupportMapFragment smf = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapaPrincipal);
if( smf != null ){
Toast.makeText(getActivity(), "Let's go!!", Toast.LENGTH_SHORT).show();
mMapa = smf.getMap();
}else{
Toast.makeText(getActivity(), "SMF is null...", Toast.LENGTH_SHORT).show();
}
if( mMapa != null ){
setupMap();
}
}
}
private void setupMap() {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "Configure the map", Toast.LENGTH_SHORT).show();
}