IllegalArgumentException onStartCommand服务

时间:2019-06-17 01:42:27

标签: android kotlin android-service illegalargumentexception

我几次从Google Play收到此错误日志,但似乎无法在测试设备上复制它。关于如何复制或解决此问题的任何建议?

java.lang.RuntimeException: 
      at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3451)
      at android.app.ActivityThread.-wrap21 (ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1653)
      at android.os.Handler.dispatchMessage (Handler.java:102)
      at android.os.Looper.loop (Looper.java:181)
      at android.app.ActivityThread.main (ActivityThread.java:6295)
      at java.lang.reflect.Method.invoke (Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:903)
      at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:793)

Caused by: java.lang.IllegalArgumentException: 
  at com.sample.tech.services.DownloadFileService.onStartCommand (DownloadFileService.kt)
  at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3434)

起初,我以为我可能会得到某种空值,尽管在日志中没有提到它,但是我什么也没想到。这是onStartCommand:

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        Log().m("In service :: Downloading File Service", "DOWNLOADING_FILE_SERVICE")
        val resourceID = intent.getIntExtra("ResourceId", 0)
        val stopDownload = intent.getBooleanExtra("StopDownload", false)
        downloadAll = intent.getBooleanExtra("downloadAll", false)

        val saveOfflineData : SaveOfflineData?
        if (resourceID != 0) {
            saveOfflineData = SaveOfflineManager().getSavedDataByID(resourceID)

            val uri = saveOfflineData?.resourceUri
            val totalContentLength = saveOfflineData?.totalContentLength
            val jsonData = saveOfflineData?.jsonData
            val fileName = saveOfflineData?.fileName

            if (!uri.isNullOrEmpty() && totalContentLength != 0.toLong() && !jsonData.isNullOrEmpty() && !fileName.isNullOrEmpty()) {
                queueDownload(saveOfflineData, stopDownload)
            }

        }

        return super.onStartCommand(intent, flags, startId)
    }

这就是我启动服务的方式:

private fun startDownload(uri : String, totalContentLength : Long, jsonData : String, fileName: String){
        if (!cancelDownload) {
            Log().m("Starting download for resourceID: $resourceID", TAG_DLOAD_VIDEO_FRAG)
            val intent = Intent(App.context, DownloadFileService::class.java)

            intent.putExtra("ResourceId", resourceID)

            val saveOfflineData = SaveOfflineData()
            saveOfflineData.resourceID = resourceID
            saveOfflineData.fragmentTag = TAG_DLOAD_VIDEO_FRAG
            saveOfflineData.startDownloadTime = startDownloadTime
            saveOfflineData.resourceUri = uri
            saveOfflineData.totalContentLength = totalContentLength
            saveOfflineData.jsonData = jsonData
            saveOfflineData.fileName = fileName
            saveOfflineData.isDownloading = true

            SaveOfflineManager().createNewEntry(saveOfflineData)

            App.context.startService(intent)
        }

    }

0 个答案:

没有答案