If you want to react to events, you need to write some actions. This video will show you how.
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4738-your-first-kotlin-android-app/lessons/25
If you want to react to events, you need to write some actions. This video will show you how.
With Android Studio version 3.3.1, we can populate the onClick property in the Attributes navigator with a method name that is to be executed when the button is tapped. Would this not be preferable to adding a listener in the onCreate method in the Activity?
@macsimus Can you please help with this when you get a chance? Thank you - much appreciated! :]
concerning method āincrementScore()ā
why do I have to store the new score into a variable ānewScoreā?
I can simply write it in the UI directly, like this:
gameScoreTextView.text = getString(R.string.your_score, score.toString())
It is working fine and I will save one line of code and a variable ā¦
Thanks for the comment @gundrabur. Itās always good to write less code for sure! But for instruction in a course like this, itās also good to add some code verbosity to add clarity for people with less development experience. Experienced developers will tend to optimize this choice.
Thanks for the question @marathoner1234. In many ways this is often up to the preference of the developer. In certain cases, itās more efficient to add the listeners in XML, in other cases, in Kotlin.
Thanks for the information. I am new to Kotlin and thought I had misunderstood something. By the way: Great workshop. I love it!
Hi, on line 29 of the tutorial, Android Studio is throwing me the error āFormat string āyour_scoreā is not a valid format string so it should not be passed to String.formatā
Upon compiling, I click the button on the emulator and my app crashes.
Any thoughts? my code looks identical to the tutorial.
Thanks!
gabe
This course hasnāt been updated yet. Itās in the works, but you want to update the line to look like the following:
val newScore = getString(R.string.your_score, Integer.toString(score))
Notice that Iām calling Integer.toString only this time, Iām passing in the score. Youāll do this in other areas as well. As always, if you get stuck, feel free to reach out.
Cheers!
Hi
Iām getting two errors while trying to run the app
Can you help in this?
lateinit is used when you donāt have the reference of a variable when the object is created. These are for variables that are not supposed to be null. By declaring a variable as a lateinit, you are telling the compiler that when the object is created, the value will be null, but that null will be gone will the object is actually used.
You are using score as an int. This contains a value. Itās not like an object that requires a reference. When using primitive types (int, double, float, bool), you provide a default value. To solve this error, you just remove the lateinit keyword because itās not needed. I hope that helps.