Java 使用支持库v4在片段中获取对Google地图的引用

Java 使用支持库v4在片段中获取对Google地图的引用,java,android,google-maps,android-fragments,android-support-library,Java,Android,Google Maps,Android Fragments,Android Support Library,我试图使用v4支持库将Google Maps添加到一个片段中,但使用getMap()引用它时遇到一些问题 我像这样添加片段(如下所示): 在mapFragmentClass中,我试图获得如下引用: public class MapFragmentClass extends Fragment { public static GoogleMap mMap; private static SupportMapFragment mapFragment; @Override

我试图使用v4支持库将Google Maps添加到一个片段中,但使用getMap()引用它时遇到一些问题

我像这样添加片段(如下所示):

在mapFragmentClass中,我试图获得如下引用:

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);

}
还是像这样

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

    Log.i("TestMap", "setCamera");
}
两个选项都返回空值。。因此,当片段处于活动状态时,基本上我没有对映射的引用。我甚至还试着在地图加载10秒后获取参考,但唉

为什么它不返回谷歌地图?var-mapFragment总是被填充的,从我在onCreateView中实例化它的那一刻起,所以如果已经填充了,我应该能够马上得到GoogleMap类型&对吗

我试着在GoogleMap中加载一个普通的活动,在布局中使用Fragement,在onCreate中,我可以实例化并引用它,没有问题。地图似乎加载速度也更快

如果有人知道如何使这个工作,我将不胜感激

编辑:我的mapfragment布局:

<?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"/>

在MainActivity中的FragmentTransaction中,您应该添加SupportMapFragment的实例,而不是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
        }
    }
}

您创建SupportMapFragment的方式只是创建一个新的片段实例,但由于您没有使用FragmentTransaction添加它,因此没有调用片段的生命周期方法。

您的R.layout.mapfragment是什么,它与您创建的
SupportMapFragment
有什么关系(但从不附加/添加任何内容)?我已在问题中发布了我的mapfragment布局。您问题的第二部分是指:“SupportMapFragment mapfragment=SupportMapFragment.newInstance();”?这不是mapFragmentClass的片段正在初始化吗?将此行
com.google.android.gms.maps.MapFragment
更改为
com.google.android.gms.maps.SupportMapFragment
我已经在使用该类:)
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
        }
    }
}