gpt4 book ai didi

java - 安卓按钮: onClick not working

转载 作者:行者123 更新时间:2023-12-01 10:36:26 26 4
gpt4 key购买 nike

我在我一直在开发的 Android 应用程序的主要 Activity 中有以下代码。由于某种原因,每当我运行该应用程序时,它就会崩溃!下面是我的 activityMain.xml(其中一部分),以及 onClick 监听器和日志错误。

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/calculate"
android:id="@+id/button"
android:textSize="20sp"
android:onClick="calculateScore"
android:layout_row="0"
android:layout_column="0"/>

我的主程序类中有以下 Java 代码:

public void calculateScore(View view) {
TextView totalScore = (TextView) findViewById(R.id.total);
totalScore.setText(totalPoints);
}

我收到此错误:

Process: org.alexwebber.frc.strongholdcalculator, PID: 8355
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
at android.view.View.performClick(View.java:5254)
at android.view.View$PerformClick.run(View.java:21179)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6895)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:5254)
at android.view.View$PerformClick.run(View.java:21179)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6895)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0xec
at android.content.res.Resources.getText(Resources.java:1399)
at android.widget.TextView.setText(TextView.java:4931)
at org.alexwebber.frc.strongholdcalculator.MainActivity.calculateScore(MainActivity.java:44)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:5254)
at android.view.View$PerformClick.run(View.java:21179)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6895)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

由于某种原因,它无法访问onClick方法。任何帮助将不胜感激!

最佳答案

onClick 代码正在正确执行。

问题是 totalPoints 是一个 int,而不是文本,因此 setText 正在寻找字符串资源 id,但找不到它。

 Caused by: android.content.res.Resources$NotFoundException: String resource ID #0xec
at android.content.res.Resources.getText(Resources.java:1399)
at android.widget.TextView.setText(TextView.java:4931)
at org.alexwebber.frc.strongholdcalculator.MainActivity.calculateScore(MainActivity.java:44)

请将您的代码更改为此

totalScore.setText("" + totalPoints);

关于java - 安卓按钮: onClick not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34702126/

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