gpt4 book ai didi

android - MapFragment 返回 null

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:22:17 25 4
gpt4 key购买 nike

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

通过这段代码 map 可见但无法访问 map

mMap=mMapFragment.getMap();显示空值错误如何解决这个问题

最佳答案

更新 1:getMap() 已弃用

最好用getMapAsync() MapFragment/SupportMapFragment 的方法。该示例如何使用下面显示的方法(从他们的 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.

在您的代码中,您在提交 MapFragment 后立即检索 GoogleMap。等到 MapFragment 完全加载到 Activity 上,这样您就可以获取 GoogleMap

或许,您可以使用接口(interface)将 GoogleMapMapFragment 传送到 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(), 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
}

}

关于android - MapFragment 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19108843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com