Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

android - 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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...