После нажатия кнопки в приложении происходит сбой, и в logcat появляется ошибка «FATAL EXCEPTION: main». К кнопке прикреплен прослушиватель событий, который должен добавить + 1 к моей переменной ScoreVar, а затем обновить строку на экране.
код MainActivity.kt
package com.nomiac.camera
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
internal lateinit var tapMeButton: Button
internal lateinit var gameScoreTextView: TextView
internal lateinit var timeLeftTextView: TextView
internal var scoreVar = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tapMeButton = findViewById<Button>(R.id.tapMeButton)
gameScoreTextView = findViewById<TextView>(R.id.gameScoreTextView)
timeLeftTextView = findViewById<TextView>(R.id.timeLeftTextView)
tapMeButton.setOnClickListener { view ->
incrementScore()
}
}
private fun incrementScore() {
scoreVar = scoreVar + 1
val newScore = getString(R.string.score, scoreVar.toString())
gameScoreTextView.text = newScore
}
}
Код strings.xml:
<resources>
<string name="app_name">Camera</string>
<string name="score">Score: % taps</string>
<string name="time">Time: % seconds</string>
<string name="button_text_start">Tap me</string>
</resources>
Ошибки Logcat:
2019-05-05 14:45:15.538 7225-7225/? E/Zygote: v2
2019-05-05 14:45:19.235 7225-7225/com.nomiac.camera E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nomiac.camera, PID: 7225
java.util.FormatFlagsConversionMismatchException: Conversion = a, Flags =
at java.util.Formatter$FormatSpecifier.failMismatch(Formatter.java:4131)
at java.util.Formatter$FormatSpecifier.checkBadFlags(Formatter.java:3035)
at java.util.Formatter$FormatSpecifier.checkDateTime(Formatter.java:3002)
at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2762)
at java.util.Formatter$FormatSpecifierParser.<init>(Formatter.java:2591)
at java.util.Formatter.parse(Formatter.java:2524)
at java.util.Formatter.format(Formatter.java:2472)
at java.util.Formatter.format(Formatter.java:2426)
at java.lang.String.format(String.java:2670)
at android.content.res.Resources.getString(Resources.java:1255)
at android.content.Context.getString(Context.java:492)
at com.nomiac.camera.MainActivity.incrementScore(MainActivity.kt:30)
at com.nomiac.camera.MainActivity.access$incrementScore(MainActivity.kt:8)
at com.nomiac.camera.MainActivity$onCreate$1.onClick(MainActivity.kt:23)
at android.view.View.performClick(View.java:6308)
at android.widget.TextView.performClick(TextView.java:11202)
at android.view.View$PerformClick.run(View.java:23969)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)