回调和协同路由Kotlin出现问题

回调和协同路由Kotlin出现问题,kotlin,mapbox,Kotlin,Mapbox,我有一些问题 在这里,我使用函数getRoute()启动一个回调,以获取导航的路径 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { Mapbox.getInstance(this.context!!, getString(R.string.token_mapbox)) mapView = view.findViewById(R.id.mapView) mapView?.onCrea

我有一些问题

在这里,我使用函数getRoute()启动一个回调,以获取导航的路径

override fun onViewCreated(view: View, savedInstanceState: Bundle?)
{
    Mapbox.getInstance(this.context!!, getString(R.string.token_mapbox))
    mapView = view.findViewById(R.id.mapView)
    mapView?.onCreate(savedInstanceState)
    Log.d("test", "test1")
    val autocomplete : GeocoderAutoCompleteView = view.findViewById(R.id.address_search)
    autocomplete.setAccessToken(Mapbox.getAccessToken())
    autocomplete.setType(GeocodingCriteria.TYPE_POI)
    autocomplete.setCountry("CA")
    autocomplete.setOnFeatureListener {
        fun onFeatureClick(feature : CarmenFeature) {
            hideOnScreenKeyboard()
            val position : Position = feature.asPosition()
            updateMap(position.latitude, position.longitude)
        }
    }
    mapView?.getMapAsync { mapboxMap ->
        map = mapboxMap
        Log.d("test", "test4")
        enableLocationPlugin()
        Log.d("test", "test3")
        originCoord = LatLng(originLocation?.latitude!!, originLocation?.longitude!!)
        Log.d("test", "test")
        Log.d("test", "test8")
        startButton.setOnClickListener(View.OnClickListener {v ->
            Log.d("test","enter here")
            val mapboxGeocoding : MapboxGeocoding = MapboxGeocoding.builder().accessToken(Mapbox.getAccessToken()!!).query(address_search.text.toString()).build()
            Log.d("test","1")
            if (destinationMarker != null) {
                mapboxMap.removeMarker(destinationMarker!!)
            }
            Log.d("test","2")
            mapboxGeocoding.enqueueCall(object : Callback<GeocodingResponse>
            {
                override fun onResponse(call : Call<GeocodingResponse>, response : Response<GeocodingResponse>) {
                    Log.d("test","3")
                    val results  = response.body()!!.features()
                    if (results.size > 0) {
                        // Log the first results Point.
                        val firstResultPoint : Point = results[0].center()!!
                        Log.d("test", "onResponse: " + firstResultPoint.toString());
                        val test = LatLng(firstResultPoint.latitude(), firstResultPoint.longitude())
                        destinationCoord = test
                        destinationMarker = mapboxMap.addMarker(MarkerOptions().position(destinationCoord))
                        destinationPosition = Point.fromLngLat(destinationCoord!!.longitude, destinationCoord!!.latitude)
                        originPosition = Point.fromLngLat(originCoord!!.longitude, originCoord!!.latitude)
                        getRoute(originPosition!!, destinationPosition!!)
                        startButton.isEnabled = true
                        startButton.isClickable = true
                        var simulateRoute: Boolean = true
                        var options: NavigationLauncherOptions = NavigationLauncherOptions.builder().directionsRoute(currentRoute).shouldSimulateRoute(simulateRoute).build()
                        NavigationLauncher.startNavigation(activity, options)
                    }
                    else {
                        // No result for your request were found.
                        Log.d(TAG, "onResponse: No result found");
                    }

                }
                override fun onFailure(call : Call<GeocodingResponse>, throwable: Throwable) {
                    throwable.printStackTrace()
                }
            })

        })
    }
}

调用
getRoute
中定义的回调可能太晚了。日志怎么说?问题是日志什么也没说,所以对我来说有点奇怪,除了currentroutes值为null之外,日志甚至没有显示“
3
”和“
onResponse:
…”在第一个示例代码块中首次使用
currentRoute
之前应该记录哪些内容?日志停在log.d(“test,testgetroute”)您的主要问题是
getRoute()
仅启动路由的检索,但在完成之前返回。可能是
getRoute()之后的代码
应该放在
onResponse
内部
getRoute()
 private fun  getRoute(origin : Point, destination : Point) {
    Log.d("test", "testgetroute")
    NavigationRoute.builder(this.context)
    .accessToken(Mapbox.getAccessToken()!!)
    .origin(origin)
    .destination(destination)
    .build()
    .getRoute( object: Callback<DirectionsResponse> {
      override fun onResponse(call : Call<DirectionsResponse>, response : Response<DirectionsResponse>) {
        // You can get the generic HTTP info about the response
        Log.d("test", "Response code: " + response.code())
        if (response.body() == null) {
          Log.e("test", "No routes found, make sure you set the right user and access token.")
          return
        } else if (response.body()!!.routes().size < 1) {
          Log.e("test", "No routes found")
          return
        }
        currentRoute = response.body()!!.routes()[0]
          Log.d("test", currentRoute.toString() + " current route is set")
          // Draw the route on the map
        if (navigationMapRoute != null) {
          navigationMapRoute!!.removeRoute()
        } else {
          navigationMapRoute = NavigationMapRoute(null, mapView!!, map!!, R.style.NavigationMapRoute)
        }
        navigationMapRoute!!.addRoute(currentRoute)
      }
        override fun onFailure(call : Call<DirectionsResponse>, throwable: Throwable) {
        Log.e("test", "Error: " + throwable.message)
      }
    })
}
   08-31 00:15:58.965 25377-25377/com.example.parky.parky_android D/test: enter here
08-31 00:15:58.971 25377-25377/com.example.parky.parky_android D/test: 1
    2
08-31 00:15:59.129 25377-25377/com.example.parky.parky_android D/test: 3
08-31 00:15:59.131 25377-25377/com.example.parky.parky_android D/test: onResponse: Point{type=Point, bbox=null, coordinates=[-71.269566, 46.779652]}
08-31 00:15:59.179 25377-25377/com.example.parky.parky_android D/test: testgetroute
08-31 00:15:59.230 25377-25377/com.example.parky.parky_android D/AndroidRuntime: Shutting down VM
08-31 00:15:59.235 25377-25377/com.example.parky.parky_android E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.parky.parky_android, PID: 25377
    java.lang.NullPointerException: Null directionsRoute
        at com.mapbox.services.android.navigation.ui.v5.AutoValue_NavigationLauncherOptions$Builder.directionsRoute(AutoValue_NavigationLauncherOptions.java:151)
        at ItemFourFragment$onViewCreated$2$1$1.onResponse(ItemFourFragment.kt:320)
        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
08-31 00:15:59.454 25377-25382/com.example.parky.parky_android I/art: Compiler allocated 8MB to compile retrofit2.ParameterHandler retrofit2.ServiceMethod$Builder.parseParameterAnnotation(int, java.lang.reflect.Type, java.lang.annotation.Annotation[], java.lang.annotation.Annotation)