当您将活动施放到Kotlin的界面时会发生什么?

发布于 2025-02-03 10:09:07 字数 814 浏览 3 评论 0原文

我有以下接口:

interface ResultReceiver {
    fun getSerialNumber(): String
    fun getDevicePassword(): String
    fun getMacAddress(): String
    fun getDevice(): BluetoothDevice
}

在对话范围内,我将附加的活动施放到我的界面上:

resultReceiver = requireActivity() as ResultReceiver

执行这种类型的铸造时会发生什么?

这是我的活动代码,可以给我的问题提供更多上下文:

class ScannerActivity: AppCompatActivity(), ResultReceiver {

    ...

    override fun getSerialNumber(): String {
        return serialNumber
    }

    override fun getDevicePassword(): String {
        return devicePassword
    }

    override fun getMacAddress(): String {
        return macAddress
    }

    override fun getDevice(): BluetoothDevice {
        return device
    }
}

我试图从活动中向我的片段发送一些字符串。

I have the following interface:

interface ResultReceiver {
    fun getSerialNumber(): String
    fun getDevicePassword(): String
    fun getMacAddress(): String
    fun getDevice(): BluetoothDevice
}

Within my DialogFragment I am casting the attached Activity to my interface like so:

resultReceiver = requireActivity() as ResultReceiver

What happens when I perform this type cast?

Here is my activity code to give a little more context to my question:

class ScannerActivity: AppCompatActivity(), ResultReceiver {

    ...

    override fun getSerialNumber(): String {
        return serialNumber
    }

    override fun getDevicePassword(): String {
        return devicePassword
    }

    override fun getMacAddress(): String {
        return macAddress
    }

    override fun getDevice(): BluetoothDevice {
        return device
    }
}

I am trying to send some Strings to my fragment from my activity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2025-02-10 10:09:07

所有活动都是从对象延伸的类。在Kotlin中,它是任何,但是在编译后,它将再次是对象。因此,您可以参加任何课程,并尝试将其投入到界面上。如果该类将与该接口没有任何关系(父子孩子),则将抛出ClassCastException

All the activities are classes which by the end extend from Object. In Kotlin it's Any but after compilation it will be Object again. So you can take any class and try to cast it to your interface. If that class will not have any relation (parent-child) with that interface, it will throw ClassCastException.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文