-1

New to Kotlin. I am trying to create a activity with a member variable holding button object, but compiler gives me error when I try to do findViewById I tried two ways for doing this but none works.

Note: I am not using Android extension

enter image description here

KR_Android
  • 1,149
  • 9
  • 19
  • https://stackoverflow.com/questions/44461571/error-on-findviewbyid-after-upgrading-to-compile-sdk-version/44461572 – duggu Feb 16 '18 at 13:18
  • @duggu this is not the same question. I have already mentioned in the question that am I not using android extension. Also correct answer is already given by zsmb13. – KR_Android Feb 16 '18 at 13:23
  • The problem here is actually due to a different issue than what you've submitted as a duplicate. – zsmb13 Feb 16 '18 at 13:24

1 Answers1

1

When you declare your property like this:

var btn = null

... its type gets inferred to Nothing?, which makes it so that nothing other than null can ever be assigned to it. What you should do instead is the following:

var btn: Button? = null

You might also want to look into declaring your Views in different ways.

zsmb13
  • 85,752
  • 11
  • 221
  • 226