早上好,我创建了一个新项目,并编写了使用浏览器的示例代码,并且该代码可以正常工作。但是现在我尝试将其复制到我的主项目中对我不起作用。错误如下:
E / Android运行时:致命异常:主要 流程:com.apparcago.pacea.firebase2login,PID:5743 java.lang.NoSuchFieldError:类Lcom / mapbox / services / android / navigation / ui / v5 / R $ id中没有类型I的静态字段NavigationView;或其超类(“ com.mapbox.services.android.navigation.ui.v5.R $ id”的声明出现在/data/app/com.apparcago.pacea.firebase2login-LSC0SaLMEkd-LbroOrauqg==/split_lib_slice_4_apk.apk)中 在com.mapbox.services.android.navigation.ui.v5.MapboxNavigationActivity.onCreate(MapboxNavigationActivity.java:32) 在android.app.Activity.performCreate(Activity.java:7098) 在android.app.Activity.performCreate(Activity.java:7089) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2768) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2893) 在android.app.ActivityThread.-wrap11(未知来源:0) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1616) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6650) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:547) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:818)
我显示了无效的项目代码:
清单
<activity
android:name=".NavigationActivity"
android:label="@string/title_activity_navigation">
</activity>
Build.gradle(模块)
android {
buildToolsVersion "27.0.3"
compileSdkVersion 27
defaultConfig {
applicationId "com.apparcago.pacea.firebase2login"
minSdkVersion 24
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
//Firebase
implementation 'com.google.firebase:firebase-core:16.0.4'
implementation 'com.google.firebase:firebase-auth:16.0.5'
implementation 'com.google.firebase:firebase-database:16.0.4'
//Facebook
implementation 'com.facebook.android:facebook-login:4.38.1'
// Para botones chulos (Fancybuttons)
implementation 'com.github.medyo:fancybuttons:1.9.0'
//Material design para Dialogs chulos
implementation 'com.afollestad.material-dialogs:core:0.9.6.0'
//ultima version me da errores
implementation 'com.google.android.gms:play-services-maps:16.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
// Material design de editText (Para hacer edit Text bonitos)
implementation 'com.rengwuxian.materialedittext:library:2.1.4'
//Diseño 27
implementation 'com.android.support:design:27.1.1'
//Cardview
implementation 'com.android.support:cardview-v7:27.1.1'
//FAB
implementation 'com.nightonke:boommenu:2.1.1'
//Mapbox
//implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:7.0.0-alpha.2'
implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:6.7.1'
implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.23.0'
}
//Firebase
apply plugin: 'com.google.gms.google-services'
Build.gradle(项目)
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.google.gms:google-services:4.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()//Para mapbox
maven { url 'https://mapbox.bintray.com/mapbox' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NavigationActivity">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView_mapbox"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="37.7749"
mapbox:mapbox_cameraTargetLng="-122.4194"
mapbox:mapbox_cameraZoom="12"
mapbox:mapbox_styleUrl="mapbox://styles/mapbox/streets-v10" />
<Button
android:id="@+id/startButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginEnd="24dp"
android:layout_marginStart="24dp"
android:enabled="false"
android:text="asd"
android:textColor="@color/colorPrimary"
mapbox:layout_constraintBottom_toBottomOf="@+id/mapView_mapbox"
mapbox:layout_constraintEnd_toEndOf="parent"
mapbox:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
NavigationActivity
package com.apparcago.pacea.firebase2login;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.android.core.permissions.PermissionsListener;
import com.mapbox.android.core.permissions.PermissionsManager;
import com.mapbox.api.directions.v5.models.DirectionsResponse;
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.location.LocationComponent;
import com.mapbox.mapboxsdk.location.modes.CameraMode;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.services.android.navigation.ui.v5.NavigationLauncher;
import com.mapbox.services.android.navigation.ui.v5.NavigationLauncherOptions;
import com.mapbox.services.android.navigation.ui.v5.route.NavigationMapRoute;
import com.mapbox.services.android.navigation.v5.navigation.NavigationRoute;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class NavigationActivity extends AppCompatActivity implements OnMapReadyCallback, MapboxMap.OnMapClickListener, PermissionsListener {
private MapView mapView;
// variables for adding location layer
private MapboxMap mapboxMap;
private PermissionsManager permissionsManager;
private Location originLocation;
// variables for adding a marker
private Marker destinationMarker;
private LatLng originCoord;
private LatLng destinationCoord;
// variables for calculating and drawing a route
private Point originPosition;
private Point destinationPosition;
private DirectionsRoute currentRoute;
private static final String TAG = "DirectionsActivity";
private NavigationMapRoute navigationMapRoute;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, getString(R.string.access_token_mapbox));
setContentView(R.layout.activity_navigation);
mapView = findViewById(R.id.mapView_mapbox);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
enableLocationComponent();
originCoord = new LatLng(originLocation.getLatitude(), originLocation.getLongitude());
mapboxMap.addOnMapClickListener(this);
button = findViewById(R.id.startButton);
button.setOnClickListener(v -> {
boolean simulateRoute = true;
NavigationLauncherOptions options = NavigationLauncherOptions.builder()
.directionsRoute(currentRoute)
.shouldSimulateRoute(simulateRoute)
.build();
// Call this method with Context from within an Activity
NavigationLauncher.startNavigation(NavigationActivity.this, options);
});
}
@Override
public void onMapClick(@NonNull LatLng point) {
if (destinationMarker != null) {
mapboxMap.removeMarker(destinationMarker);
}
destinationCoord = point;
destinationMarker = mapboxMap.addMarker(new MarkerOptions()
.position(destinationCoord)
);
destinationPosition = Point.fromLngLat(destinationCoord.getLongitude(), destinationCoord.getLatitude());
originPosition = Point.fromLngLat(originCoord.getLongitude(), originCoord.getLatitude());
getRoute(originPosition, destinationPosition);
button.setEnabled(true);
button.setBackgroundResource(R.color.mapboxBlue);
}
private void getRoute(Point origin, Point destination) {
NavigationRoute.builder(this)
.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());
}
});
}
@SuppressWarnings( {"MissingPermission"})
private void enableLocationComponent() {
// Check if permissions are enabled and if not request
if (PermissionsManager.areLocationPermissionsGranted(this)) {
// Activate the MapboxMap LocationComponent to show user location
// Adding in LocationComponentOptions is also an optional parameter
LocationComponent locationComponent = mapboxMap.getLocationComponent();
locationComponent.activateLocationComponent(this);
locationComponent.setLocationComponentEnabled(true);
// Set the component's camera mode
locationComponent.setCameraMode(CameraMode.TRACKING);
Location lastLocation = locationComponent.getLastKnownLocation();
originLocation = locationComponent.getLastKnownLocation();
} else {
permissionsManager = new PermissionsManager(this);
permissionsManager.requestLocationPermissions(this);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public void onExplanationNeeded(List<String> permissionsToExplain) {
Toast.makeText(this, "PRUEBA1", Toast.LENGTH_LONG).show();
}
@Override
public void onPermissionResult(boolean granted) {
if (granted) {
enableLocationComponent();
} else {
Toast.makeText(this, "PERMISOS NO ACEPTADOS", Toast.LENGTH_LONG).show();
finish();
}
}
@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
工程中唯一的区别在于,由于它是一个仅用于学习mapbox的项目,因此依赖项较少。
我找不到错误,但是我认为可能与gradle有关,有什么建议吗?
对不起,我的英语敬意,谢谢。
答案 0 :(得分:0)
只需将您的类名称(NavigationActivity)和布局名称(activity_navigation)重构为另一个。它与具有相同类和布局名称的mapbox类冲突