当您将活动施放到Kotlin的界面时会发生什么?
我有以下接口:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有活动都是从
对象
延伸的类。在Kotlin中,它是任何
,但是在编译后,它将再次是对象
。因此,您可以参加任何课程,并尝试将其投入到界面上。如果该类将与该接口没有任何关系(父子孩子),则将抛出ClassCastException
。All the activities are classes which by the end extend from
Object
. In Kotlin it'sAny
but after compilation it will beObject
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 throwClassCastException
.