質問

I'm trying to add Google Maps to a fragment using the v4 support lib, but I have some trouble using getMap() to reference it.

I'm adding the fragment like so (following the example at http://developer.android.com/training/basics/fragments/fragment-ui.html#AddAtRuntime) :

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (findViewById(R.id.fragment_container) != null) {

            if (savedInstanceState != null) {
                return;
            }

            MapFragmentClass mapFragmentClass= new MapFragmentClass ();
            mapFragmentClass.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, mapFragmentClass).commit();
        }
    }
}

And in the mapFragmentClass I'm trying to get the reference like this:

public class MapFragmentClass extends Fragment {

    public static  GoogleMap mMap;
    private static SupportMapFragment mapFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        SupportMapFragment mapFragment = SupportMapFragment.newInstance();
        mMap = mapFragment.getMap();

        return inflater.inflate(R.layout.mapfragment, container, false);

}

or like this

@Override
public void  onResume(){
    super.onResume();
    mMap = mapFragment.getMap();

    Log.i("TestMap", "setCamera");
}

Both options come back with a null.. So basically I don't have a reference to the map when the Fragment is active. I even also tried getting a reference 10 seconds after the map was loaded, but alas..

Why won't it return a GoogleMap? The var mapFragment is always filled, from the moment I instantiate it in onCreateView, so if that's filled, I should be able to get a GoogleMap type back right then & there, correct?

I've tried loading in a GoogleMap a normal Activity, using a Fragement in the layout, and there, in onCreate, I can instantiate and reference it, no problem. The map seems to load in faster as well.

If anybody has any idea how to get this working, I'd be much obliged.

Edit: My mapfragment layout:

<?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/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"
          tools:layout="@layout/main"/>
役に立ちましたか?

解決

In your FragmentTransaction in MainActivity you should add an instance of SupportMapFragment instead of MapFragmentClass.

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (findViewById(R.id.fragment_container) != null) {

            SupportMapFragment mapFragmentClass= new SupportMapFragment ();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, mapFragmentClass).commit();
            getSupportFragmentManager().executePendingTransactions();
            GoogleMap map = mapFragmentClass.getMap();
            // do what you need to do with the map
        }
    }
}

The way you were creating SupportMapFragment was just creating a new Fragment instance but since you didn't add it using a FragmentTransaction none of the lifecycle methods of the Fragment were being called.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top