gpt4 book ai didi

java - 更改字符串后获取 "FATAL EXCEPTION: main"

转载 作者:行者123 更新时间:2023-12-01 19:39:09 27 4
gpt4 key购买 nike

在我的应用程序中按下按钮后,它崩溃了,我在 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)

最佳答案

更改此行

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

到此

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

您必须指定 arg 的格式。对你来说它的 %s 意味着字符串

关于java - 更改字符串后获取 "FATAL EXCEPTION: main",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55991820/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com