【问题标题】:Android Studio error - Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type ComponentName? [duplicate]Android Studio 错误 - 在 ComponentName 类型的可空接收器上只允许安全 (?.) 或非空断言 (!!.) 调用? [复制]
【发布时间】:2020-11-14 21:29:23
【问题描述】:

如果有人能帮我解决以下问题,我将不胜感激。

我正在尝试在 android studio 中构建 APK,但遇到了错误。

错误:

e: D:\WT\ZY\3.0\app\src\main\java\com\app\zy\cameraX\cameraxbasic\utils\Utils.kt: (117, 34): Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type ComponentName?

这段代码是:

    fun isAppIsInBackground(context: Context): Boolean {
        var isInBackground = true
        val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
            val runningProcesses = am.runningAppProcesses
            for (processInfo in runningProcesses) {
                if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    for (activeProcess in processInfo.pkgList) {
                        if (activeProcess == context.packageName) {
                            isInBackground = false
                        }
                    }
                }
            }
        } else {
            val taskInfo = am.getRunningTasks(1)
            val componentInfo = taskInfo[0].topActivity
            if (componentInfo.packageName == context.packageName) {
                isInBackground = false
            }
        }

错误行是

    if (componentInfo.packageName == context.packageName) {

如果有人可以帮助我,不胜感激。

谢谢

【问题讨论】:

标签: android


【解决方案1】:

您可以将 If 子句更改为

if (componentInfo != null && componentInfo.packageName == context.packageName)

【讨论】:

  • 谢谢.........
  • 如果确实对您有所帮助,请标记答案,以便其他人可以找到相同的内容
猜你喜欢
  • 2018-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-24
相关资源
最近更新 更多