android – MapFragment返回null

android – MapFragment返回null,第1张

概述mMapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentByTag(MAP_FRAGMENT_TAG); // We only create a fragment if it doesn't already exist. if (mMa
mMapFragment = (SupportMapFragment) getSupportFragmentManager()                .findFragmentByTag(MAP_FRAGMENT_TAG);        // We only create a fragment if it doesn't already exist.        if (mMapFragment == null) {          mMapFragment = SupportMapFragment.newInstance();            // Then we add it using a FragmentTransaction.            FragmentTransaction fragmentTransaction =                    getSupportFragmentManager().beginTransaction();            fragmentTransaction.add(MapLay.getID(),mMapFragment,MAP_FRAGMENT_TAG);             fragmentTransaction.commit();            mMap=mMapFragment.getMap();

通过此代码映射可见但无法访问地图

MMAP = mMapFragment.getMap();显示空值错误如何解决此问题

解决方法 更新1:不推荐使用getMap()

最好使用MapFragment / SupportMapFragment的getMapAsync()方法.示例如何使用下面显示的方法(从他们的documentation复制).

import com.Google.androID.gms.maps.*;import com.Google.androID.gms.maps.model.*;import androID.app.Activity;import androID.os.Bundle;public class MapPane extends Activity implements OnMapReadyCallback {    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.map_activity);        MapFragment mapFragment = (MapFragment) getFragmentManager()                .findFragmentByID(R.ID.map);        mapFragment.getMapAsync(this);    }    @OverrIDe    public voID onMapReady(GoogleMap map) {        LatLng sydney = new LatLng(-33.867,151.206);        map.setMyLocationEnabled(true);        map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,13));        map.addMarker(new MarkerOptions()                .Title("Sydney")                .snippet("The most populous city in Australia.")                .position(sydney));    }}

引用Google的MapFragment/SupportMapFragment

A GoogleMap can only be acquired using getMap() when the underlying
maps system is loaded and the underlying vIEw in the fragment exists.
This class automatically initializes the maps system and the vIEw;
however you cannot be guaranteed when it will be ready because this
depends on the availability of the Google Play services APK. If a
GoogleMap is not available,getMap() will return null.

在您的代码中,您可以立即检索GoogleMap AFTER Committing MapFragment.等到MapFragment完全加载到活动上,这样您就可以获得GoogleMap.

也许,您可以使用界面将MapMap从MapFragment传递到Activity,就像这样.

public class MyMapFragment extends SupportMapFragment{  private MapCallback callback;  public voID setMapCallback(MapCallback callback)  {    this.callback = callback;  }  public static interface MapCallback  {     public voID onMapReady(GoogleMap map);  }  @OverrIDe public voID onActivityCreated(Bundle savedInstanceState)  {     super.onActivityCreated(savedInstanceState);     if(callback != null) callback.onMapReady(getMap());       }}public class MyActivity extends Activity implements MyMapFragment.MapCallback{   // .........  @OverrIDe  public voID onCreate(Bundle onsavedInstanceState)  {        mMapFragment = (MyMapFragment) getSupportFragmentManager()                .findFragmentByTag(MAP_FRAGMENT_TAG);        // We only create a fragment if it doesn't already exist.        if (mMapFragment == null) {               mMapFragment = MyMapFragment.newInstance();               mMapFragment.setMapCallback(this); // This activity will receive the Map object once the map fragment is fully loaded               // Then we add it using a FragmentTransaction.               FragmentTransaction fragmentTransaction =                    getSupportFragmentManager().beginTransaction();               fragmentTransaction.add(MapLay.getID(),MAP_FRAGMENT_TAG);               fragmentTransaction.commit();         }         else         {               mMapFragment.setMapCallback(this); // This activity will receive the Map object once the map fragment is fully loaded         }  @OverrIDe  public voID onMapReady(GoogleMap map)  {     // Do what you want to map  }}
总结

以上是内存溢出为你收集整理的android – MapFragment返回null全部内容,希望文章能够帮你解决android – MapFragment返回null所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1134464.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存