我在我的主要活动中有以下代码,我正在研究一个Android应用程序。出于某种原因,每当我运行应用程序时,它都会崩溃!我的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
方法。任何帮助都会很有价值!
答案 0 :(得分:1)
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);
答案 1 :(得分:0)
我认为你将整数值传递给calculateScore()中的setText方法。试试这个::
public void calculateScore(View view) {
TextView totalScore = (TextView) findViewById(R.id.total);
totalScore.setText(String.valueOf(totalPoints));
}