将文本设置为整数值 [英] Set Text to Integer Value

查看:17
本文介绍了将文本设置为整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 TextView 设置为整数的值,希望是这样:

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)

推荐答案

一般来说,

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

如果值在另一个类中?您可以为该类中所需的值创建一个 getter:

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:

但是如果您将 TextView 设置为 int,它将被解释为 Android 资源 id.如果您希望 int 的值作为文本(而不是它指向的资源),请先将其设为 String.

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 如果您的 int 是 firstResult 根据您在下面的评论,那么 getter 将变为:

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

public int getFirstResult() {
    return firstResult;
}

这篇关于将文本设置为整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆