【问题标题】:MapView null in Fragment片段中的 MapView 为空
【发布时间】:2015-07-01 13:24:06
【问题描述】:

我有这个 xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background="#f00"
android:gravity="center">

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

</LinearLayout>

这是我在 Fragment 的 onCreateView 方法中的代码

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.map_activity, container, false);
    mapView = (MapView) view.findViewById(R.id.map);
    mMap = mapView.getMap();

      if (mMap== null)
        {
            Toast.makeText(getActivity().getBaseContext(),"Google Maps not Available",
                    Toast.LENGTH_LONG).show();
        }

       locReader = (LocationReader) new LocationReader(getActivity().getBaseContext(), true);
       this.loc = locReader.getLastKnownLocation(); 
       moveCamMyPosition(loc);

    return view;
}

片段加载正常,但 nMap 为空。 Toast 显示“Google 地图不可用”。

我找不到错误。

【问题讨论】:

  • 你有没有在menifest中创建和定义密钥
  • 您在哪个设备上运行此代码?好像你在使用模拟器。
  • @aldakur 错误的布局夸大了map_activity。片段的布局是否相同??
  • 是的。这个名字不合适。在 Activity 的最后 y 启动地图中,但现在我想要在 Fragment 中进行布局

标签: android dictionary fragment


【解决方案1】:

尝试使用getMapAsync(OnMapReadyCallback callback) 而不是getMap()http://developer.android.com/reference/com/google/android/gms/maps/MapView.html

public class myFragment extends Fragment implements OnMapReadyCallback {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        /* ... */
        mapView.getMapAsync(this);
        /* ... */
    }

    @Override
    public void onMapReady(GoogleMap map) {
        /* do something with the map */
        mMap = map;
        locReader = (LocationReader) new LocationReader(getActivity().getBaseContext(), true);
        this.loc = locReader.getLastKnownLocation(); 
        moveCamMyPosition(loc);
    }
}

【讨论】:

  • nMap = mapView.getMapAsync(this) "mapView 类型的 getMapAsync(OnMapReadyCallback callback) 方法不适用于参数 myFragmentName"
  • 但在这种情况下,我的 mMap 变量也是 null。
  • onMapReady 中设置map = mMap,然后你就可以对你的地图做一些事情了。
  • 我写了登录onMapReady方法,没有运行
【解决方案2】:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CHD = new LatLng(dlat, dlong);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
    map.setMyLocationEnabled(true);
    map.addMarker(new MarkerOptions().position(CHD).title("You Are Here"));
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(CHD, 15));
    map.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);

}





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

【讨论】:

    【解决方案3】:

    您是否生成了正确的 Google Maps API 密钥?

    而谷歌地图需要以下权限。

    • ACCESS_NETWORK_STATE – 检查网络状态是否可以下载数据
    • INTERNET – 检查互联网连接状态
    • WRITE_EXTERNAL_STORAGE – 写入外部存储,因为谷歌地图将地图数据存储在外部存储中
    • ACCESS_COARSE_LOCATION – 使用 WiFi 和移动蜂窝数据确定用户的位置
    • ACCESS_FINE_LOCATION – 使用 GPS 确定用户的位置

    OpenGL ES V2 - 谷歌地图 V2 所必需的

    【讨论】:

    • 是的。 Activity 中发布的最后一张地图
    猜你喜欢
    • 2014-11-28
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 2016-04-18
    • 2018-03-24
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多