Home > Software design >  Android Studio: How do you make a method when pressing a button with Kotlin?
Android Studio: How do you make a method when pressing a button with Kotlin?

Time:05-24

I've tried making the below method for a button on Android Studio, and when I run, the app crashes.

MainActivity : AppCompatActivity() {

    val button = findViewById(R.id.button) as Button
    var count = 0
    val textView = findViewById(R.id.textView) as TextView

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

        button.setOnClickListener{
            buttonAction()
        }
    }

    fun buttonAction(){
        count = count 1
        textView.text = count.toString()
    }
}

I am well aware that in order for the button to work, you need to put stuff into the "button.setOnClickListener" part (see below). However, I want to make sure that my code is as neat and clean as possible by calling the method inside the "button.setOnClickListener" rather than put every stuff into that part.

MainActivity : AppCompatActivity() {

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

        val button = findViewById(R.id.button) as Button
        var count = 0
        val textView = findViewById(R.id.textView) as TextView

        button.setOnClickListener{
            count = count 1
            textView.text = count.toString()
        }
    }
}

Is there any fixes for the 1st code, or would I have to settle for the 2nd code?

CodePudding user response:

class MainActivity : AppCompatActivity() {

    private var count = 0
    private var button: Button? = null
    private var textView: TextView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button = findViewById(R.id.button)
        textView = findViewById(R.id.textView)
        button?.setOnClickListener {
            buttonAction()
        }
    }

    private fun buttonAction() {
        count  = 1
        textView?.text = count.toString()
    }
}

CodePudding user response:

The reason for your app crash is that you're trying to assign views(button and textView) that do not (yet) exist by calling them before onCreate.

val button = findViewById(R.id.button) as Button
val textView = findViewById(R.id.textView) as TextView
override fun onCreate(savedInstanceState: Bundle?) { ...

Just put this lines inside onCreate after setContentView(R.layout.activity_main).

You can think of it as trying to shake your friend's hand before meeting him.

And about making code clean, I wouldn't worry about it too much, your second solution is good enough for a beginner.

Not sure if you're aware of Android Basics in Kotlin course, but it can help you a lot if you follow it step by step.

And just to give you an answer, here is a code

class MainActivity : AppCompatActivity() {

var count = 0

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

    val button = findViewById(R.id.button) as Button
    val textView = findViewById(R.id.textView) as TextView

    button.setOnClickListener{
        buttonAction()
        textView.text = count.toString()
    }
}

fun buttonAction(){
    count  = 1
   }
}

It's not the smoothest solution, but as i said, it's good enough

  • Related