更改字符串后获取“致命异常:主”

时间:2019-05-05 12:15:08

标签: java android kotlin

在我的应用程序中按下按钮后,它崩溃了,并且在logcat中出现“致命异常:主要”错误。该按钮上附加了一个事件侦听器,该事件侦听器应为我的scoreVar变量加+1,然后更新屏幕上的字符串。

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)

2 个答案:

答案 0 :(得分:2)

更改此行

step3

对此

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

您必须指定arg的格式。对您来说,它的%s表示字符串

答案 1 :(得分:1)

在Kotlin上并不是很流利,但这条线似乎并不正确。 getString()应该接收(至少在经典的 Android Java 中)资源ID,因此逗号后的内容应该会引起您的问题。

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

应该吗 val newScore = getString(R.string.score) + scoreVar.toString()吗?