0

I have the following statement in Java

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

and I tried to convert into Kotlin code and I have

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

but I have the next error in

unregisterReceiver(mBroadcastReceiver)

Variable mBroadcastReceiver must be initialized

1 Answer 1

1

try passing this instead of the mBroadcastReceiver into the function:

unregisterReceiver(this)

Sign up to request clarification or add additional context in comments.

1 Comment

It work's! i'm feel like a dummy, I forgot it and I use it instead of this (facepalm)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.