将文本设置为整数值

编程入门 行业动态 更新时间:2024-10-28 04:15:54
本文介绍了将文本设置为整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将 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; }

更多推荐

将文本设置为整数值

本文发布于:2023-07-29 08:25:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239055.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   设置为   文本

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!