我在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必须初始化
答案 0 :(得分:1)
尝试将this
而不是mBroadcastReceiver
传递给函数:
unregisterReceiver(this)