gpt4 book ai didi

java - 将 Java 代码转换为 Kotlin 代码时出错

转载 作者:行者123 更新时间:2023-12-02 10:03:08 25 4
gpt4 key购买 nike

我在Java中有以下语句

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
unregisterReceiver(mBroadcastReceiver);
}
};

我尝试转换为 Kotlin 代码,结果

private var mBroadcastReceiver:BroadcastReceiver = 
object:BroadcastReceiver(){
override fun onReceive(context: Context, intent: Intent) {
unregisterReceiver(mBroadcastReceiver)
stopSelf()
}
}

但是我有下一个错误

unregisterReceiver(mBroadcastReceiver)

Variable mBroadcastReceiver must be initialized

最佳答案

尝试将 this 而不是 mBroadcastReceiver 传递到函数中:

取消注册接收器(this)

关于java - 将 Java 代码转换为 Kotlin 代码时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55484296/

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