Kotlin - 期望类型为Task <drivefile>的值!

时间:2018-02-13 15:55:23

标签: android kotlin

我尝试使用此功能将文件添加到应用的AppFolder中:

private fun syncListToDrive() {
        val jsonList = Gson().toJson(activeSubs)

        val appFolderTask = mDriveResourceClient?.appFolder
        val createContentsTask = mDriveResourceClient?.createContents()
        Tasks.whenAll(appFolderTask, createContentsTask)
                .continueWithTask(com.google.android.gms.tasks.Continuation<Void, Task<DriveFile>> {
                    override fun then(task:Task<Void>):Task<DriveFile> {
                        val parent = appFolderTask?.result
                        val contents = createContentsTask?.result
                        val outputStream = contents?.outputStream

                        val writer = OutputStreamWriter(outputStream)
                        writer.write(jsonList)

                        val changeSet = MetadataChangeSet.Builder()
                                .setTitle("list")
                                .setMimeType("json")
                                .setStarred(true)
                                .build()

                        return mDriveResourceClient.createFile(parent!!, changeSet, contents)
                    }
                })
    }

具体而言then函数有一个错误,编译器将其描述为

  

Expected a value of type Task<DriveFile>!

对我来说看起来就像是函数返回的那个。我错过了什么?

查看API我发现createFile()函数返回Task<DriveFile!>!

2 个答案:

答案 0 :(得分:0)

我忘了使用Object Expression

替换:

Tasks.whenAll(appFolderTask, createContentsTask)
                .continueWithTask(com.google.android.gms.tasks.Continuation<Void, Task<DriveFile>> {}

使用:

Tasks.whenAll(appFolderTask, createContentsTask)
                .continueWithTask(object: com.google.android.gms.tasks.Continuation<Void, Task<DriveFile>> {}

我解决了这个问题。

答案 1 :(得分:0)

以下是最短的表格

Tasks.whenAll(appFolderTask, createContentsTask)
        .continueWithTask({ task ->

        })