【问题标题】:Creating google maps v2 in a fragment using Navigation Drawer使用导航抽屉在片段中创建谷歌地图 v2
【发布时间】:2015-08-13 09:57:55
【问题描述】:

我正在尝试在 Fragment 中创建地图,但不断收到此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
        at com.new.newapps.activity.MapsFragment.initilizeMap(MapsFragment.java:41)
        at com.new.newapps.activity.MapsFragment.onResume(MapsFragment.java:52)

我不确定如何解决这个问题。请看下面我的代码。

MapsFragment 类:

public class MapsFragment extends Fragment {

public MapsFragment(){}
GoogleMap googleMap;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_distance, container, false);
    try {
        initilizeMap();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rootView;
}

private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((SupportMapFragment) getFragmentManager()
                .findFragmentById(R.id.map))
                .getMap();
        if (googleMap == null) {
            Toast.makeText(getActivity(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}
@Override
public void onResume() {
    super.onResume();
    initilizeMap();
    if (googleMap != null) {

    }
    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(xx.xxxx, xx.xxxx))
            .zoom(10)
            .bearing(0)
            .tilt(80)
            .build();

    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onDetach() {
    super.onDetach();
}
}

然后在我的布局文件中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">

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


</RelativeLayout>

这是我的 gradle 文件:

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

请原谅我的英语不好:-)

任何帮助将不胜感激!谢谢

【问题讨论】:

    标签: android google-maps android-fragments


    【解决方案1】:

    这是对this comment的回复

    所以基本上,人们使用SupportMapFragment#getMap() 来获取GoogleMap 对象已经很长时间了。并且有太多的问题/答案/讨论/解决方法/技巧来摆脱它可能产生的 NPE。

    最后,Google 迈出了正确的一步,您可以在这里看到:deprecate that getMap() method and add getMapAsync() instead

    因此,包含MapView/MapFragment 的应用程序/片段必须实现com.google.android.gms.maps.OnMapReadyCallback 接口并覆盖其onMapReady(GoogleMap map) 以获取Map 对象。这个想法是 GoogleMap 对象需要时间来初始化,所以如果你调用已弃用的 getMap() 方法,你得到的可能是一个 NULL 对象。它会导致 NPE。所以正确的方法是异步获取它并设置回调来检索非空对象

    我创建了a github repo: Google Map Practice 来帮助您了解如何轻松实现它,并寻求更好的方法。我真的很惊讶 Android Studio 默认创建一个 Google Map Activity (在 repo 中是 DefaultMapsActivity )使用已弃用的 getMap() 。这可能是因为他们很长时间没有更新他们的旧模板。但是由于 Google 会通知您有关新 API 的信息,请看一下。加油!

    P/S:如果你想试试我的 repo,别忘了在 /app/src/release/res/values/google_maps_api.xml/app/src/debug/res/values/google_maps_api.xml 中设置你的 Google Api Key

    【讨论】:

      【解决方案2】:

      我认为你犯了一个小错误,请查看 OnResume 方法:

      @Override
      public void onResume() {
          super.onResume();
          initilizeMap();
          if (googleMap != null) {
      
          }
      }
      

      把这个改成,

      @Override
      public void onResume() {
          super.onResume();
      
          if (googleMap != null) {
              initilizeMap();
          }
      }
      

      【讨论】:

      • @Wildness 很高兴为您提供帮助。
      • 如果地图不为空,为什么还需要初始化地图。这个答案有效,因为您在该方法中检查地图是否为空,所以 sn-p 什么都不做。
      • @NguyễnHoàiNam 我只是建议他在哪里做错了。请先检查我的答案以及我在其中写的内容。
      • @Shvet 你在调用方法之前检查这个: googleMap != null ,但在那个方法中,他检查 googleMap 是否为 null 然后运行其余的。所以基本上你的修复什么都不做——>没有崩溃。我稍后会更新答案,但考虑使用最新的 google play 服务库,如果您在片段中添加地图,请使用 MapView 而不是 SupportMapFragment,并使用 getMapAsync 正确获取地图对象。
      • @NguyễnHoàiNam 请检查此URL。我已经实现了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 2014-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多