gpt4 book ai didi

java - Mapbox Android 中空对象引用上的 NavigationView

转载 作者:行者123 更新时间:2023-12-01 16:46:19 25 4
gpt4 key购买 nike

我想在路线之间导航。我已成功创建两点之间的路线,但在尝试在它们之间开始导航时遇到错误。类似于 null 对象引用上的 NavigationView.onCreate(android.os.Bundle) 。我怎样才能做到这一点以及我在哪里做错了?

public class NavigationActivity extends AppCompatActivity implements OnMapReadyCallback{

public static final String TAG = "NavigationActivity";
private MapView mapView;
private TDD tdd;
private Point originPosition, dstPosition;
private Double originLat, originLng, dstLat, dstLng;
private LatLng originLatLng, dstLatLng;
private MapboxMap map;
private NavigationMapRoute navigationMapRoute;
private DirectionsRoute currentRoute;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, getString(R.string.access_token));
setContentView(R.layout.activity_navigation);
MapBox(savedInstanceState);
// originLatLng = new LatLng(24.8955, 67.0271);
// dstLatLng = new LatLng(24.9180, 67.0971);

}

@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
this.map = mapboxMap;

mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {

originPosition = Point.fromLngLat(originLatLng.getLongitude(), originLatLng.getLatitude());
dstPosition = Point.fromLngLat(dstLatLng.getLongitude(), dstLatLng.getLatitude());

MarkerOptions options = new MarkerOptions();
options.title("Source");
options.position(originLatLng);

MarkerOptions options1 = new MarkerOptions();
options1.title("Destination");
options1.position(dstLatLng);

mapboxMap.addMarker(options);
mapboxMap.addMarker(options1);
getRoute(originPosition, dstPosition);

LatLngBounds latLngBounds = new LatLngBounds.Builder()
.include(originLatLng) // Northeast
.include(dstLatLng) // Southwest
.build();

mapboxMap.easeCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 50), 5000);


}
});


}

private void MapBox(Bundle savedInstanceState)
{
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);

Intent i = getIntent();
tdd = (TDD) i.getSerializableExtra("tdd");

originLat = Double.parseDouble(tdd.getmSrcLat());
originLng = Double.parseDouble(tdd.getmSrcLng());
dstLat = Double.parseDouble(tdd.getmDstLat());
dstLng = Double.parseDouble(tdd.getmDstLng());

originLatLng = new LatLng(originLat, originLng);
dstLatLng = new LatLng(dstLat, dstLng);
}

private void getRoute(Point origin, Point destination)
{
NavigationRoute.builder(NavigationActivity.this)
.accessToken(Mapbox.getAccessToken())
.origin(origin)
.destination(destination)
.build()
.getRoute(new Callback<DirectionsResponse>() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
if (response.body() == null)
{
Usable.logMessage(TAG, "No routes found, Check User and Access Token..");
return;
} else if (response.body().routes().size() == 0)
{
Usable.logMessage(TAG, "No routes found..");
return;
}


Log.e(TAG, "Direction Route: "+ response.body().routes().size());

currentRoute = response.body().routes().get(0);
navigationMapRoute = new NavigationMapRoute(null, mapView, map);
navigationMapRoute.addRoute(currentRoute);

navigationRoute(currentRoute);

}

@Override
public void onFailure(Call<DirectionsResponse> call, Throwable t) {
Log.e(TAG, "Error: "+ t.getMessage());

}
});

}

private void navigationRoute(DirectionsRoute cr)
{
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
NavigationLauncherOptions options = NavigationLauncherOptions.builder()
.directionsRoute(cr)
.shouldSimulateRoute(true)
.build();
NavigationLauncher.startNavigation(NavigationActivity.this, options);
}
}, 5000);
}


@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(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}
<LinearLayout 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"
android:orientation="vertical"
android:weightSum="100"
tools:context=".NavigationActivity">


<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="90"
mapbox:mapbox_cameraTargetLat="24.8607"
mapbox:mapbox_cameraTargetLng="67.0011"
mapbox:mapbox_cameraZoom="11"
/>

<Button
android:id="@+id/startNavigation"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:text="Start Navigation"
android:textColor="@color/white"
android:layout_margin="2dp"
android:enabled="false"
android:background="@drawable/ripple_effect_button"/>

</LinearLayout>
2020-05-13 13:05:58.199 20171-20171/com.devaj.googlemaps E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.devaj.googlemaps, PID: 20171
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.devaj.googlemaps/com.mapbox.services.android.navigation.ui.v5.MapboxNavigationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mapbox.services.android.navigation.ui.v5.NavigationView.onCreate(android.os.Bundle)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3073)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7025)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mapbox.services.android.navigation.ui.v5.NavigationView.onCreate(android.os.Bundle)' on a null object reference
at com.mapbox.services.android.navigation.ui.v5.MapboxNavigationActivity.onCreate(MapboxNavigationActivity.java:32)
at android.app.Activity.performCreate(Activity.java:7258)
at android.app.Activity.performCreate(Activity.java:7249)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1222)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2941)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3073) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:7025) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408) 

最佳答案

这对我有用。我假设您缺少依赖项。

请确保将其包含到您的 build.gradle 文件中:

项目级别 build.gradle:

allprojects {
repositories {
google()
jcenter()
maven { url 'https://mapbox.bintray.com/mapbox' }

}
}

应用程序级别build.gradle:

dependencies {
implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.42.6'
}

如果有帮助,请告诉我!

关于java - Mapbox Android 中空对象引用上的 NavigationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61769594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com