代码之家  ›  专栏  ›  技术社区  ›  Hello World

将Java Lambda转换为Kotlin Lamba时出错

  •  1
  • Hello World  · 技术社区  · 7 年前

    我想用Fetch2在我的应用程序中下载一个文件,但是我在尝试时遇到了这个错误。

    This link ]

        fetch.enqueue(request, updatedRequest -> {
            //Request was successfully enqueued for download.
        }, error -> {
            //An error occurred enqueuing the request.
        });
    

        fetch.enqueue(request,
        success = { _: com.tonyodev.fetch2.Request ->
            TODO()        
        },
        failed = {  _: com.tonyodev.fetch2.Error ->
            TODO()
        })
    

    这是我得到的错误: Image

    编辑: 我在编译代码时遇到了这个错误。

    None of the following functions can be called with the arguments supplied:
    public abstract fun enqueue(request: Request, func: Func<Request>? = ..., func2: Func<Error>? = ...): Fetch defined in com.tonyodev.fetch2.Fetch
    public abstract fun enqueue(requests: List<Request>, func: Func<List<Request>>? = ..., func2: Func<Error>? = ...): Fetch defined in com.tonyodev.fetch2.Fetch
    
    4 回复  |  直到 7 年前
        1
  •  1
  •   GianhTran    7 年前

    试试看

      fetch.enqueue(request,
        success = { 
            TODO()        
        },
        failed = { 
            TODO()
        })
    

        2
  •  1
  •   Vladimir Gladun    7 年前

    您检查过您在这里尝试使用的方法中的签名和javadoc吗?

    /**
         * Queues a request for downloading. If Fetch fails to enqueue the request,
         * func2 will be called with the error.
         * Errors that may cause Fetch to fail the enqueue are :
         * 1. No storage space on the device.
         * 2. Fetch is already managing the same request. This means that a request with the same url
         * and file name is already managed.
         * @param request Download Request
         * @param func Callback that the enqueued request will be returned on.
         *             Fetch may update a request depending on the initial request's Enqueue Action.
         *             Update old request references with this request.
         * @param func2 Callback that is called when enqueuing a request fails. An error is returned.
         * @throws FetchException if this instance of Fetch has been closed.
         * @return Instance
         * */
        fun enqueue(request: Request, func: Func<Request>? = null, func2: Func<Error>? = null): Fetch
    
    /**
     * Queues a list of requests for downloading. If Fetch fails to enqueue a
     * download request because an error occurred, all other request in the list will
     * fail. Func2 will be called with the error message.
     * Errors that may cause Fetch to fail the enqueue are :
     * 1. No storage space on the device.
     * 2. Fetch is already managing the same request. This means that a request with the same url
     * and file name is already managed.
     * @param requests Request List
     * @param func Callback that the enqueued request will be returned on.
     *             Fetch may update a request depending on the initial request's Enqueue Action.
     *             Update old request references with this request.
     * @param func2 Callback that is called when enqueuing a request fails. An error is returned.
     * @throws FetchException if this instance of Fetch has been closed.
     * @return Instance
     * */
    fun enqueue(requests: List<Request>, func: Func<List<Request>>? = null, func2: Func<Error>? = null): Fetch
    

    所以第二个和第三个参数基本上是回调,你甚至可以跳过或调用类似的fetch.排队(request,object:Func{//在此处实现回调方法

        3
  •  1
  •   SaurabhS    7 年前

    目前,在将javasam转换为kotlinlambdas时,必须显式地指定类型。因此,您的代码应该类似于:

    fetch.enqueue(request,
        success = Func<Request> { _: com.tonyodev.fetch2.Request ->
            TODO()        
        },
        failed = Func<Error> {  _: com.tonyodev.fetch2.Error ->
            TODO()
        })
    
        4
  •  1
  •   tryp    7 年前

    fetch.enqueue(request, Func {
        // success
    }, Func {
       // failure
    })
    
        5
  •  0
  •   Hello World    7 年前

    我现在可以解决我的问题,即使我不知道它是如何工作的。

        fetch.enqueue(request)
            Func<Request> { _ ->
                //TODO()
            }
            Func<com.tonyodev.fetch2.Error> { _ ->
                //TODO()
    

        fetch.enqueue(request,
            Func { _ ->
                //TODO()
            },
            Func { _ ->
                //TODO()
            })