NoSuchMethodError:没有虚拟方法getMapAsync

时间:2017-01-18 22:11:19

标签: java android google-maps

我试图重振我在2014年制作的旧Google地图应用。除了一个之外没有任何问题:尝试在我在上面调用的自定义方法中初始化地图同一个班级,我收到这个错误:

java.lang.NoSuchMethodError: No virtual method getMapAsync(Lcom/google/android/gms/maps/OnMapReadyCallback;)V in class Lcom/google/android/gms/maps/MapFragment; or its super classes (declaration of 'com.google.android.gms.maps.MapFragment' appears in /data/data/com.packagename.appname/files/instant-run/dex/slice-google-play-services_013f7ca48f60d837bd691f2b8fafce962adefa56-classes.dex)

这是方法本身:

private void initilizeMap()
{
    if (mGoogleMap == null) {
        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            mGoogleMap.setMyLocationEnabled(true);
        } else {
            Toast.makeText(this, "You have to accept to enjoy all app's services!", Toast.LENGTH_LONG).show();
        }
    }
}

方法initializeMap()在其声明之上调用,Android Monitor指向该行以及该行:

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);

片段在布局中正确声明。另外,在下面,我已经覆盖了mapReady上的方法:

@Override
public void onMapReady(GoogleMap googleMap)
{
    mGoogleMap = googleMap;
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        mGoogleMap.setMyLocationEnabled(true);
    }
}

我不知道造成这个问题的原因。

EDIT(build.gradle依赖项):

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:design:25.1.0'
    compile 'com.google.maps.android:android-maps-utils:0.4+'
    testCompile 'junit:junit:4.12'
    compile files('libs/android-google-maps-api13.jar')
    compile 'com.google.android.gms:play-services:8.4.0'
    apply plugin: 'com.google.gms.google-services'
    compile files('libs/google-play-services.jar')
    compile 'com.google.android.gms:play-services-maps:8.4.0'
}

1 个答案:

答案 0 :(得分:0)

您在运行时看到的MapFragment实例没有预期的方法。看起来您使用较新版本进行编译,然后用于运行应用程序。