【问题标题】:Kotlin inferred type mismatchKotlin 推断类型不匹配
【发布时间】:2018-10-08 08:18:33
【问题描述】:

我尝试使用 FirebaseJobDispatcher 设置 JobService。

这是我的 JobService:

class MyJobService : JobService() {
override fun onStartJob(job: JobParameters): Boolean {
    // Do some work here
   return false //return false if job done otherwise return true
}

override fun onStopJob(job: JobParameters): Boolean {
    return false //Should this job be retried?"
   }
}

但是,当我尝试这样设置时:

val dispatcher = FirebaseJobDispatcher(GooglePlayDriver(this))
val myJob = dispatcher.newJobBuilder()
    .setService(MyJobService::class.java) // the JobService that will be called
    .setTag("my-unique-tag")        // uniquely identifies the job
    .build()

我在 Android Studio 中遇到此编译器错误:

类型推断失败。预期类型不匹配:推断类型是 Class 但 Class!预料之中的

如何在 Kotlin 中正确设置?

【问题讨论】:

  • 你应该尝试.setService(MyJobService::class).setService(MyJobService::class.java!!)(为了符合Class!)我猜。
  • 我之前都试过了 - 没用
  • 您使用的是最新版本吗? firebase-jobdispatcher:0.8.5' ?我在他们的存储库中看到了Improve Kotlin support (#193)。根据我阅读的内容,您的代码似乎是正确的。
  • 是的,我使用的正是那个版本。我也创建了一个问题:github.com/firebase/firebase-jobdispatcher-android/issues/231
  • 嘿,我也面临同样的问题!有什么更新吗?

标签: android kotlin


【解决方案1】:

我好像导入了错误的JobService。

代替:

import android.app.job.JobParameters
import android.app.job.JobService

应该是:

import com.firebase.jobdispatcher.JobParameters
import com.firebase.jobdispatcher.JobService

【讨论】:

    猜你喜欢
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    • 2021-11-18
    • 1970-01-01
    • 2020-07-06
    • 2018-04-22
    相关资源
    最近更新 更多