gpt4 book ai didi

android - Kotlin 中的 (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE) 等价于什么?

转载 作者:行者123 更新时间:2023-12-02 12:42:40 26 4
gpt4 key购买 nike

我正在尝试在 kotlin 中获取系统布局 inflater:如果我将 LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 转换为 kotlin,我会得到 val inflater = getSystemService(Context .LAYOUT_INFLATER_SERVICE) as LayoutInflater? 但它似乎是错误的。那么 kotlin 中的等价物是什么?

最佳答案

这是等价的。你只有一个 null-protected cast,以防服务为 null(它不应该是,但它偶尔会发生)。

要调用您的 inflater 变量,您只需使用 inflater?.method()

或者,只需将 as LayoutInflater? 更改为 as LayoutInflater 即可避免使用空安全语法。

作为另一种选择,您可以只使用 LayoutInflater.from(this),这基本上只是一个方便的函数,但它避免了您必须强制转换。

关于android - Kotlin 中的 (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE) 等价于什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52261955/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com