【问题标题】:MapFragment - NullPointerException - Google Maps V2 - AndroidMapFragment - NullPointerException - Google Maps V2 - Android
【发布时间】:2013-05-16 15:36:00
【问题描述】:

我收到一个 NullPointerException 异常,代码如下:

private ProjectionProxy proxy = new ProjectionProxy();
private GoogleMap mMap = null;
private Context context = this;

@Override
protected void onCreate(Bundle load) { 
    super.onCreate(load);

    setContentView(R.layout.fragment_ride_tracking);

    //mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.myMapView)).getMap();

    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

    int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);

    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mMap.setMyLocationEnabled(true);

    //SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.myMapView);
    //mMap = mapFragment.getMap();

    Log.e("RideTracking", "Google Map VALUE:"+mMap);

    if (mMap != null) { 
        proxy.setProjection(mMap.getProjection());
    }

发生 NullPointerException 的行是这行代码:

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

我没有将我的 mMap 变量初始化为 null。我的问题是什么?

【问题讨论】:

  • 你在使用支持库吗?
  • 是的,我正在使用支持库
  • 不确定但试试这个mMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
  • 你确定你的布局中有一个有效的片段吗?似乎“findFragment”检索到的片段为空。但是没有布局文件和堆栈跟踪,我无话可说。

标签: android google-maps android-maps-v2


【解决方案1】:

MainActivity.xml:

<?xml version="1.0" encoding="utf-8"?>
<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.SupportMapFragment"/>

MainActivity.java:

private GoogleMap googleMap;
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

【讨论】:

  • 这不是一个完整的答案。如果您知道 OP 的代码有什么问题,请详细说明。
  • GigoNet Gigolashvili 的答案是正确的。重要的区别是:如果您使用 API 级别
【解决方案2】:

尝试检查您在 Activity 的 XML 中创建的 MapFragment。检查我的;它正在工作。

...

<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

...

你的不一样吗?此外,您检查了您是否在 AndroidManifest.xml 上放置了正确的权限和元数据?

这里是许可:

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

这是元数据部分:

<application>

    ...

    <meta-data android:name="com.google.android.maps.v2.API_KEY"
               android:value="HERE GOES YOUR API KEY" />

</application>

希望对你有帮助!

【讨论】:

    【解决方案3】:

    您可能在 manifest.xml 中缺少此内容
    &lt;meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/&gt;

    【讨论】:

      猜你喜欢
      • 2013-01-05
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      • 2013-12-21
      • 1970-01-01
      • 2015-03-14
      相关资源
      最近更新 更多