MapFragment返回null [英] MapFragment return null
问题描述
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();
通过此code图可见,但无法访问地图
By this code map is visible but unable to access the map
MMAP = mMapFragment.getMap();显示空值错误怎么解决这个问题。
mMap=mMapFragment.getMap(); show null value error how to fix this
推荐答案
更新1: 的GetMap()
是德precated
这是更好地使用<一个href="http://developer.android.com/reference/com/google/android/gms/maps/SupportMapFragment.html#getMapAsync(com.google.android.gms.maps.OnMa$p$padyCallback)"><$c$c>getMapAsync()$c$c> MapFragment / SupportMapFragment
的方法。该示例演示如何使用如下所示的方法(从文档复制)。
It is better to use getMapAsync()
method of MapFragment/SupportMapFragment
. The example how to use the method shown below (copied from their 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));
}
}
引用谷歌的<一href="http://developer.android.com/reference/com/google/android/gms/maps/MapFragment.html">MapFragment/SupportMapFragment
一个GoogleMap的只能使用的GetMap()当底层被收购 地图系统被加载并在片段的底层视图存在。 此类自动初始化的地图系统和视图; 但是你不能保证什么时候会准备好,因为这 依赖于谷歌播放服务APK的可用性。如果一个 GoogleMap的不可用,的GetMap()将返回null。
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.
在你的code,你马上取回 GoogleMap的
MapFragment
后犯下。等待,直到 MapFragment
的活动上满载这样你就可以得到 GoogleMap的
。
On your code, you immediately retrieve GoogleMap
AFTER Committing MapFragment
. Wait until the MapFragment
is fully loaded on activity so you can get the GoogleMap
.
也许,你能提供的 GoogleMap的
从 MapFragment
到活动
使用的接口,是这样的。
Perhaps, you can deliver the GoogleMap
from MapFragment
to Activity
using interface, like this.
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(), mMapFragment, 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
}
}
这篇关于MapFragment返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!