在初始化谷歌地图时,mapFragment.getMapAsync(this)处的空指针异常

时间:2015-01-23 11:46:07

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

当我使用Google Play服务的v6.5.87时,我在调用getMapAsync()时会收到空指针异常。我在SupportMapFragment的xml布局中使用Fragment

我的代码:

SupportMapFragment mapFragment = (SupportMapFragment) getActivity()
        .getSupportFragmentManager().findFragmentById(R.id.mapFragment);

mapFragment.getMapAsync(this);

注意:在较旧的Google Play Services版本上调用getMap()时没有问题,但getMap()现已弃用(docs)。

这是NPE的堆栈跟踪:

java.lang.NullPointerException
            at com.decos.fixi.fragments.FragmentLocationMap.initializeMap(FragmentLocationMap.java:132)
            at com.decos.fixi.fragments.FragmentLocationMap.onCreateView(FragmentLocationMap.java:95)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
            at dalvik.system.NativeStart.main(Native Method)

5 个答案:

答案 0 :(得分:32)

最后我得到了答案!

SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                .findFragmentById(R.id.mapFragment);

而不是

SupportMapFragment mapFragment = (SupportMapFragment) this.getSupportFragmentManager()
                .findFragmentById(R.id.mapFragment);

getChildFragmentManager()完成了这个伎俩。

答案 1 :(得分:3)

此外,如果您使用SupportMapFragment mapFragment = (SupportMapFragment).....

然后确保您在class

中设置了此XML属性
class="com.google.android.gms.maps.SupportMapFragment"

class ="com.google.android.gms.maps.MapFragment"

答案 2 :(得分:1)

在AppCompatActivity扩展

FragmentManager mFragmentManager = getSupportFragmentManager();
SupportMapFragment mVTMapFragment =(SupportMapFragment)mFragmentManager.findFragmentById(R.id.mapRideview);
mVTMapFragment.getMapAsync(this);

并实现onMapReady回调方法

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    setUpMap();
}

答案 3 :(得分:0)

您是否为getMapAsync()实施了回调功能?

如果没有,您需要在代码中实施onMapReady()方法,然后您可以在onMapReady()方法中添加标记或移动相机内容。

您可以看到关于onMapReady()的{​​{3}}。

答案 4 :(得分:0)

标题

通过添加此

来运行您的程序
 SupportMapFragment supportMapFragment
ON CREATE METHOD()

添加

supportMapFragment = SupportMapFragment.newInstance(); 

FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.maplayout, new MapFragmentClass()).commit();
supportMapFragment.getMapAsync(this);

android.support.v4.app.FragmentManager sfm = getSupportFragmentManager();
sfm.beginTransaction().add(R.id.map,supportMapFragment).commit();

这将检查