0

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!

4
  • 2
    Please do not use PasteBin for your errors, you can include it in your question like the code. Commented Jan 10, 2016 at 4:42
  • What is totalpoints ? Is it string and has it initialised ? Commented Jan 10, 2016 at 5:06
  • Please paste the code where you initialized the code for button and set the onclicklistener Commented Jan 10, 2016 at 5:15
  • @IchigoKurosaki - You don't need Java code for initializing the button or the onclicklistener if it is defined in the XML Commented Jan 10, 2016 at 5:47

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.