After pressing a button in my application it crashes and I get the "FATAL EXCEPTION: main" error in the logcat. An event listener is attached to the button which should add + 1 to my scoreVar variable and then update the string on the screen.

The code of 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
    }

}

The code of 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 errors:

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)

2 Answers

2
Radesh On Best Solutions

Change this line

 <string name="score">Score: % taps</string>

to this

 <string name="score">Score: %s taps</string>

You must specify format of arg. for you its %s means string

1
razgraf On

Not really fluent in Kotlin but this line doesn't seem right. getString() should receive (at least in classic Android Java) the resource ID, so the stuff after the comma should be causing your problem.

val newScore = getString(R.string.score, scoreVar.toString()

Shouldn't it be val newScore = getString(R.string.score) + scoreVar.toString() ?