Android Studio 3.0-alpha8中的Kotlin apply()扩展lint消息 [英] Kotlin apply() extension lint message in Android Studio 3.0-alpha8

查看:164
本文介绍了Android Studio 3.0-alpha8中的Kotlin apply()扩展lint消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码会产生以下棉绒错误.

I have following code that produces following lint error.

fun newInstance(message: String?): DialogFragment {
    return DialogFragment().apply {
        arguments = Bundle().apply {
            putString("arg", message)
        }
    }
}

该消息指出apply()函数内的this引用指向BaseBundle类,该类自API 21起可用,它将在较低的API上崩溃. 捆绑软件#在较低版本中肯定可以使用putString(key,value),但是Android Studio 3.0-alpha8中存在错误.

The message points out that this reference inside apply() function points to BaseBundle class that is available since API 21 which will crash on lower API. Bundle#putString(key, value) is definitely available on lower versions, but there is an error in Android Studio 3.0-alpha8.

这个问题很奇怪,因为我可以看到反编译后的代码是这样的:

The issue quite strange as I can see decompiled code as this:

引用哪个Bundle类型而不是BaseBundle.

为什么我们首先出现Lint错误?

Why do we have Lint error in first place?

推荐答案

一种解决方法是使用let代替apply,例如:

One workaround is to use let instead of apply, like:

fun newInstance(message: String?): DialogFragment {
    return DialogFragment().apply {
        arguments = Bundle().let {
            it.putString("arg", message)
            it
        }
    }
}

这篇关于Android Studio 3.0-alpha8中的Kotlin apply()扩展lint消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆