无法在Google Maps 10.0.1中解析方法getMap()

时间:2016-12-08 13:52:57

标签: android google-maps google-play android-mapview

我有一个旧的Android项目,我留下了,我已经决定拿起它。但我得到了#34;无法解决方法getMap()"错误。我曾尝试过做hereherehere,但我还是被卡住了。我知道我应该使用getMapAsync,但我想我没能用MapView

实现它

我想也许是因为我使用MapView而不是SupportMapFragment。我也试过改变它,但我一路上都没有。

以下是我想要使用的依赖项,或者更确切地说是使用:

compile 'com.google.android.gms:play-services-maps:10.0.1' //one with error
//compile 'com.google.android.gms:play-services-maps:7.3.0' //one which works

我有一些被标记的方法,因为我不止一次使用getMap(),但错误标记在这里:

GoogleMap map = ((MapView) mRootView.findViewById(R.id.mapview)).getMap();

MapView显示如下:

<com.google.android.gms.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

我收到以下错误:

12-09 09:14:33.231 15831-15831/com.empire.vince.vokers.yoworld E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.empire.vince.vokers.yoworld, PID: 15831
                                                                             java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.maps.android.clustering.ClusterManager.clearItems()' on a null object reference
                                                                                 at com.empire.vince.vokers.yoworld.mafragments.MapFragment.renderView(MapFragment.java:465)
                                                                                 at com.empire.vince.vokers.yoworld.mafragments.MapFragment.onMapReady(MapFragment.java:433)
                                                                                 at com.google.android.gms.maps.MapView$zza$1.zza(Unknown Source)
                                                                                 at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
                                                                                 at android.os.Binder.transact(Binder.java:387)
                                                                                 at zu.a(:com.google.android.gms.DynamiteModulesB:82)
                                                                                 at maps.ad.t$5.run(Unknown Source)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:234)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5526)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这是4的第一个使用GoogleMap对象的函数:

private void gimmeView()
{
    // reference

   //final GoogleMap map = ((MapView) mRootView.findViewById(R.id.mapview)).getMap();
    final AdView adView = (AdView) mRootView.findViewById(R.id.adview);

    // map
    if(mGoogleMap!=null)
    {
        // add pois
       mGoogleMap.clear();
        mClusterManager.clearItems();
        for(PoiModel poi : mPoiList)
        {
            mClusterManager.addItem(poi);
        }
        mClusterManager.cluster();
    }

    // admob
    if(YoWorldConfig.ADMOB_MAP_BANNER && NetworkManager.isOnline(getActivity()))
    {
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice(getString(R.string.admob_test_device_id))
                .build();
        adView.loadAd(adRequest);
        adView.setVisibility(View.VISIBLE);
    }
    else
    {
        adView.setVisibility(View.GONE);
    }
}

1 个答案:

答案 0 :(得分:0)

不要将MapView投射到GoogleMap,这是你应该做的:

private GoogleMap mGoogleMap;

OnCreate方法:

mMapView = (MapView) view.findViewById(R.id.mapview);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
mMapView.getMapAsync(this);

实施OnMapReadyCallback

@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
if (mGoogleMap != null) {
    // Add your functions to GoogleMap
 }
}