【问题标题】:Unable to stop service with IndexOutOfBoundsException无法使用 IndexOutOfBoundsException 停止服务
【发布时间】:2020-01-17 12:53:10
【问题描述】:

我的应用出现了有趣的崩溃。我添加了 4 个最常见的崩溃日志,但我无法解释它们。有没有人遇到过类似的问题? p.s 我没有在我的代码中的任何地方使用ArrayList

Fatal Exception: java.lang.RuntimeException: Unable to stop service workers.BackgroundPlayer@c853ae2: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

Caused by java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
   at java.util.ArrayList.remove(ArrayList.java:477)
   at androidx.lifecycle.LifecycleRegistry.popParentState(LifecycleRegistry.java:202)
   at androidx.lifecycle.LifecycleRegistry.backwardPass(LifecycleRegistry.java:317)
   at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:334)
   at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:145)
   at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:131)
   at androidx.lifecycle.ServiceLifecycleDispatcher$DispatchRunnable.run(ServiceLifecycleDispatcher.java:105)
   at androidx.lifecycle.ServiceLifecycleDispatcher.postDispatchRunnable(ServiceLifecycleDispatcher.java:45)
   at androidx.lifecycle.ServiceLifecycleDispatcher.onServicePreSuperOnDestroy(ServiceLifecycleDispatcher.java:81)
   at androidx.lifecycle.LifecycleService.onDestroy(LifecycleService.java:70)
   at workers.BackgroundPlayer.onDestroy(streamPlayer.kt:385)

Fatal Exception: java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1
   at java.util.ArrayList.remove(ArrayList.java:486)
   at androidx.lifecycle.LifecycleRegistry.popParentState(LifecycleRegistry.java:202)
   at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:301)
   at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:339)
   at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:145)
   at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:131)
   at androidx.lifecycle.ServiceLifecycleDispatcher$DispatchRunnable.run(ServiceLifecycleDispatcher.java:105)
   at android.os.Handler.handleCallback(Handler.java:761)
   at android.os.Handler.dispatchMessage(Handler.java:98)
   at android.os.Looper.loop(Looper.java:156)
   at android.app.ActivityThread.main(ActivityThread.java:6523)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)

Fatal Exception: java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1
   at java.util.ArrayList.add(ArrayList.java:468)
   at androidx.lifecycle.LifecycleRegistry.pushParentState(LifecycleRegistry.java:206)
   at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:299)
   at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:339)
   at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:145)
   at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:131)
   at androidx.lifecycle.ServiceLifecycleDispatcher$DispatchRunnable.run(ServiceLifecycleDispatcher.java:105)

服务类:

class BackgroundPlayer : LifecycleService() {

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    super.onStartCommand(intent, flags, startId)

    return START_NOT_STICKY
}

// Start service player

fun start() {
    Thread {
        run {
            settingsLastPlayed(station)

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) startForegroundService(Intent(this, BackgroundPlayer::class.java).apply { putExtra(station) })
            else                                                startService(Intent(this, BackgroundPlayer::class.java)          .apply { putExtra(station) })
        }
    }.start()
}

override fun onDestroy() {
    releasePlayer()
    cancelRecording()

    super.onDestroy()
}

private fun releasePlayer() {
    mediaSession             ?.release()
    mediaSessionConnector    ?.setPlayer(null)
    playerNotificationManager?.setPlayer(null)
    exoPlayer                ?.release()
}

}

【问题讨论】:

  • 什么时候出现这些崩溃?什么时候启动服务?此外,如果您使用前台服务,您还应该向用户 developer.android.com/guide/components/services#Foreground 显示通知。编辑:另外,您真的需要 LifecycleService,请尝试使用 Service。
  • 这可能是 api 实现中的一个错误 - 谷歌搜索时发现了一些示例。它在调用 super.onDestroy 时发生 - 这可能是您的代码试图在该事件上做的最后一件事。如果是这种情况,您唯一能做的就是在super.destroy() 上捕获异常以自定义错误报告并抑制异常的传播。但是,它可能会在内部分派给 looper,这意味着您无法捕获异常 - 因此在这种情况下什么也做不了。
  • @Cata 我在 firebase crashlytics 上遇到崩溃,没有更多信息......何时何地为什么。这就是我掌握的所有信息。我还显示通知,是的,我需要 LifecycleService,因为我使用 RX,我需要取消订阅 observables 等。
  • @Andy 我不知道当我用 try catch 包装 super.destroy() 时它是否对我有帮助

标签: android service indexoutofboundsexception exoplayer foreground-service


【解决方案1】:

确保您调用所有方法,如

Lifecycle.addObserver()

Lifecycle.removeObserver()

MainThread。 检查代码中调用这些方法的任何位置。

【讨论】:

    猜你喜欢
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多