我正在学习创建MapBox导航应用程序的教程。当我到达获得Route的点时,我复制整个代码部分,然后检查currentRoute类以查看我是否确实获得了路由,并且它返回了DirectionsRoute类的方向和其他调用,所以我假设我有准备好添加的路线。一切似乎都很好,直到我尝试将currentRoute添加到NavigationMapRoute。然后我收到错误java.lang.NoSuchMethodError: No direct method <init>(Ljava/lang/String;Lcom/mapbox/services/commons/geojson/FeatureCollection;Lcom/mapbox/mapboxsdk/style/sources/GeoJsonOptions;)V in class Lcom/mapbox/mapboxsdk/style/sources/GeoJsonSource; or its super classes (declaration of 'com.mapbox.mapboxsdk.style.sources.GeoJsonSource' appears in /data/app/MyApp-IHmaY_75jKlOywZrW4r3Ow==/base.apk)
我很困惑,因为当我完成所有设置时,我不希望在教程部分中打电话失败。我在这里错过了什么? (代码取自https://www.mapbox.com/help/android-navigation-sdk/#calculate-and-draw-route) `
private void getRoute(com.mapbox.geojson.Point origin, com.mapbox.geojson.Point destination) {
NavigationRoute.builder()
.accessToken(Mapbox.getAccessToken())
.origin(origin)
.destination(destination)
.build()
.getRoute(new Callback<DirectionsResponse>() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
// You can get the generic HTTP info about the response
Log.d(TAG, "Response code: " + response.code());
if (response.body() == null) {
Log.e(TAG, "No routes found, make sure you set the right user and access token.");
return;
} else if (response.body().routes().size() < 1) {
Log.e(TAG, "No routes found");
return;
}
currentRoute = response.body().routes().get(0);
// Draw the route on the map
if (navigationMapRoute != null) {
navigationMapRoute.removeRoute();
} else {
navigationMapRoute = new NavigationMapRoute(null, mapView, mapboxMap, R.style.NavigationMapRoute);
}
navigationMapRoute.addRoute(currentRoute);
}
@Override
public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
Log.e(TAG, "Error: " + throwable.getMessage());
}
});
}
此代码位于我的onCreate()
中destinationPosition = com.mapbox.geojson.Point.fromLngLat(-122.269532,37.879842);
originPosition = com.mapbox.geojson.Point.fromLngLat(-122.258875, 37.865593);
getRoute(originPosition, destinationPosition);
Build.Gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "MyApp"
minSdkVersion 22
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
mavenCentral()
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.android.gms:play-services-location:11.0.4'
implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:5.5.1'
implementation "com.mapzen.android:lost:3.0.2"
implementation ('com.mapbox.mapboxsdk:mapbox-android-sdk:6.0.1@aar') {
transitive=true
}
implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation:0.12.0'
implementation ('com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.12.0') {
transitive = true
}
implementation 'com.android.support:design:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
完整错误:
java.lang.NoSuchMethodError: No direct method <init>(Ljava/lang/String;Lcom/mapbox/services/commons/geojson/FeatureCollection;Lcom/mapbox/mapboxsdk/style/sources/GeoJsonOptions;)V in class Lcom/mapbox/mapboxsdk/style/sources/GeoJsonSource; or its super classes (declaration of 'com.mapbox.mapboxsdk.style.sources.GeoJsonSource' appears in /data/app/Me.MyApp-ALhfKnyL4l3OfYHrn1IyAQ==/base.apk)
at com.mapbox.services.android.navigation.ui.v5.utils.MapUtils.updateMapSourceFromFeatureCollection(MapUtils.java:43)
at com.mapbox.services.android.navigation.ui.v5.route.NavigationMapRoute.drawRoutes(NavigationMapRoute.java:307)
at com.mapbox.services.android.navigation.ui.v5.route.NavigationMapRoute.addRoutes(NavigationMapRoute.java:244)
at com.mapbox.services.android.navigation.ui.v5.route.NavigationMapRoute.addRoute(NavigationMapRoute.java:226)
at Me.MyApp.MainActivity$1.onResponse(MainActivity.java:231)
at com.mapbox.api.directions.v5.MapboxDirections$1.onResponse(MapboxDirections.java:167)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案 0 :(得分:2)
解决方案:从gradle文件中删除两行代码,然后再次同步,清理并构建。
implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:5.5.1'
implementation ('com.mapbox.mapboxsdk:mapbox-android-sdk:6.0.1@aar') {
transitive=true
}