简体   繁体   中英

Android button: onClick not working

I have the following code in my main activity for an android application that I have been working on. For some reason, whenever I run the app it crashes! My activityMain.xml (a portion of it) is found below, as well as the onClick listener, and the log error.

<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"/>

I have the following Java code in my main program class:

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

And I get this error:

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)

For some reason, it cant get to the onClick method. Any help would be much appriceated!

The onClick code is properly being executed.

The problem is totalPoints is an int , not text, so setText is looking for a string resource id, but it cannot find it.

 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)

Please change your code to this

totalScore.setText("" + totalPoints);

I think you are passing integer value to setText method in calculateScore(). Try this ::

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM