我在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)



  变量mBroadcastReceiver必须初始化

最佳答案

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

unregisterReceiver(this)

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

10-10 14:32