【问题标题】:getMap() is null in SupportMapFragment in google map in android fragmentgetMap() 在 Android 片段中谷歌地图的 SupportMapFragment 中为空
【发布时间】:2016-10-09 17:10:27
【问题描述】:

我在片段中使用谷歌地图并在调用 getMap() 时为空,这是我的代码:

在我的 MapViewFragment 中

    private GoogleMap mGoogleMap;

在 onCreate() 方法中,我在这一行得到了 null...

        mGoogleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

在 fragment_map_view 文件中

    <fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

【问题讨论】:

标签: android google-maps android-layout


【解决方案1】:

我知道原因了,我想我应该为以后的读者发帖......

其实我 GoogleMap 是在一个片段中调用的,所以,在行中..

mGoogleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

而不是

getFragmentManager(), getSupportFragmentManager() should be used in fragments.

更正一个-

mGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

【讨论】:

  • 您应该接受上述给出的答案之一,因为它包含与您解决的相同的解决方案:)。
  • 你是对的,他们解决了,但当时最短的解决方案在上面。
【解决方案2】:

您可以使用getMapAsync 来执行此操作,并且在onMapReady 方法中您将获得地图对象。

public class YourMapActivity extends FragmentActivity implements OnMapReadyCallback {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...

    ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
}

 @Override
public void onMapReady(final GoogleMap googleMap) {

   // you will get map object hare 
    mGoogleMap = googleMap;

   // now perform operations on that.
   }
}

【讨论】:

    【解决方案3】:

    您是否添加了权限和 Google API 密钥?

    这是我在 OnCreate() 中调用此函数的项目代码

    private void CheckGoogleMap()
        {
    
            try {
                if (googleMap == null) {
                    googleMap = ((MapFragment) getFragmentManager().
                            findFragmentById(R.id.map)).getMap();
                }
                googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    

    在 XML 文件中

    <fragment
                    android:id="@+id/map"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    class="com.google.android.gms.maps.MapFragment"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true">
    

    在清单文件中

     <uses-permission android:name="android.permission.INTERNET" />
            <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
            <uses-permission android:name="com.example.googlemaps.permission.MAPS_RECEIVE" />
            <uses-permission android:name="android.permission.INTERNET"/>
    
    
        <permission         android:name="com.example.googlemaps.permission.MAPS_RECEIVE" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-feature    android:glEsVersion="0x00020000" android:required="true" />
    

    在 Manifest 文件中关闭应用程序 TAG 之前添加此

     <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="YOUR API KEY" />
    

    在 Gradle 中添加

    compile 'com.google.android.gms:play-services:8.1.0'
    

    【讨论】:

      【解决方案4】:

      getMap 方法已弃用。你应该使用getMapAsync:

      public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              // ...
      
              ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
          }
      
           @Override
          public void onMapReady(final GoogleMap googleMap) {
              mGoogleMap = googleMap;
      
              // Do something with your map
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-04
        • 1970-01-01
        • 2017-08-16
        • 2016-04-09
        • 1970-01-01
        • 1970-01-01
        • 2020-12-21
        • 1970-01-01
        相关资源
        最近更新 更多