1

I am working on a android studio project which is a hybrid of kotlin and java. I am trying to set an onClickListener to a button, however I keep running into compile time errors like the following:

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.folioreader.android.sample/com.folioreader.ui.activity.FolioActivity}:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

This is my code:

 private var buttonBookmarks: Button? = null

 //these two are in onCreate
 var buttonBookmarks = findViewById<Button>(R.id.btn_bookmarks)

 buttonBookmarks.setOnClickListener {
        Toast.makeText(this, "Works", LENGTH_LONG).show()
    }

I know that this is an easy question, however, I did not have any experience with Kotlin before starting to work on this so I find it a bit confusing. Some online solutions I have tried did not work so I am asking the question here. What is wrong with my code?

D. PRAKASH
  • 305
  • 1
  • 12
Vasko Vasilev
  • 554
  • 1
  • 10
  • 25

3 Answers3

4

In Kotlin you do not need findViewById.

btn_bookmarks.setOnClickListener {
        Toast.makeText(this, "Works", LENGTH_LONG).show()
    }

Should work fine by the help of kotlin-synthetic if still error then you should look the ID and make sure they are matched.

Note: To use kotlin-synthetic you should apply plugins like following

apply plugin: 'kotlin-android-extensions'

For more info about extension visit here

Jasurbek
  • 2,946
  • 3
  • 20
  • 37
2

This error

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.folioreader.android.sample/com.folioreader.ui.activity.FolioActivity}:

Says that your activity is not loaded correctly, do you have something like this?

class FolioActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.folio_activity) <-- this one
    }
}

Once you have set the content view you will can use your setOnClickListener as before.

 buttonBookmarks.setOnClickListener {
    Toast.makeText(this, "Works", LENGTH_LONG).show()
 }
Ashish
  • 6,791
  • 3
  • 26
  • 48
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • 1
    If someone is downvoting, please add a reason :) – Skizo-ozᴉʞS ツ Jun 25 '19 at 08:10
  • 1
    This worked, this line of code: setContentView(R.layout.folio_activity) was a bit below in the onCreate method. I just placed your code below it and it worked. – Vasko Vasilev Jun 25 '19 at 08:12
  • @Skizo-ozᴉʞS reason for downvote : context has nothing to with his error. there is problem with `setContentView(R.layout.folio_activity)`and better use `Toast.LENGTH_LONG` without importing another package. – Ashish Jun 25 '19 at 09:45
  • @Ashish I've edited the question, I removed the context stuff and now is a decent answer, feel free to remove the downvote ;) – Skizo-ozᴉʞS ツ Jun 07 '21 at 07:01
0
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        private var buttonBookmarks: Button? = null

        //these two are in onCreate
        var buttonBookmarks = findViewById<Button>(R.id.btn_bookmarks)

        buttonBookmarks.setOnClickListener {
            Toast.makeText(this, "Works", LENGTH_LONG).show()
        }
    }
}

I just tried your code. It works perfectly. Your syntax seems correct, By seeing your Error

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

only thing is your buttonBookmarks is null reason may be you are either missing the button in your activity's XML file or you are referencing the wrong element.

  • Comment private var buttonBookmarks: Button? = null too. Hope It helps Thanks
Jasurbek
  • 2,946
  • 3
  • 20
  • 37
CodeWithVikas
  • 1,105
  • 9
  • 33