MapFragment - NullPointerException - Google Maps V2 - Android [英] MapFragment - NullPointerException - Google Maps V2 - Android

查看:25
本文介绍了MapFragment - NullPointerException - Google Maps V2 - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个带有以下代码的 NullPointerException:

I'm getting an NullPointerException with the following code:

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 的那一行是这行代码:

The line where the NullPointerException is happening is this line of code:

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

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

I'm not initializing my mMap variable to null. What is my issue here?

推荐答案

尝试检查您在活动的 XML 中创建的 MapFragment.检查我的;它正在工作.

Try to check the MapFragment that you've created in the XML of the Activity. Check mine; it's working.

...

<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 上放置了正确的权限和元数据?

Is yours different? Also, you checked if you put the correct permissions and metadata on the 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>

希望对你有帮助!

这篇关于MapFragment - NullPointerException - Google Maps V2 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆