Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Text to Integer Value

I want to set a TextView as the value of an integer, hopefully like so:

tv.setText(int)

I tried this an I get this error.

Also, my integer value is in another class

D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.application, PID: 29603
  java.lang.IllegalStateException: Could not execute method for android:onClick
      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
      at android.view.View.performClick(View.java:5207)
      at android.view.View$PerformClick.run(View.java:21168)
      at android.os.Handler.handleCallback(Handler.java:746)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5443)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
   Caused by: java.lang.reflect.InvocationTargetException
      at java.lang.reflect.Method.invoke(Native Method)
      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
      at android.view.View.performClick(View.java:5207) 
      at android.view.View$PerformClick.run(View.java:21168) 
      at android.os.Handler.handleCallback(Handler.java:746) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.app.ActivityThread.main(ActivityThread.java:5443) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
   Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
      at android.content.res.Resources.getText(Resources.java:312)
      at android.widget.TextView.setText(TextView.java:4427)
like image 546
MPKenning Avatar asked Jan 08 '12 21:01

MPKenning


2 Answers

Generally,

tv.setText(String.valueOf(int));

If the value is inside another class? You can make a getter for the value you want in that class:

public int getValue() {
    return value;
}

So that you can access it from the other one:

BUT if you set a TextView to an int, it will be interpreted as an Android resource id. If you want the value of the int as your text (and not the resource it points to), make it a String first.

tv.setText(String.valueOf(theOtherClassInstance.getValue()));

EDIT If your int is firstResult as per your comment below, then the getter becomes:

public int getFirstResult() {
    return firstResult;
}
like image 63
Guillaume Avatar answered Nov 04 '22 19:11

Guillaume


Doing setText(int) you are refering to an application resource from the XML files, not the value itself.

To set the integer propertly do the following:

tv.setText(""+integer);

or better solution:

tv.setText(String.valueOf(integer));
like image 43
Corbella Avatar answered Nov 04 '22 20:11

Corbella