如何在android中以编程方式将数字放到textview中 [英] how to put a number to the textview programatically in android

查看:137
本文介绍了如何在android中以编程方式将数字放到textview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临一个小而愚蠢的错误,我无法找到。在我的主类中,我声明了一个变量int a = 4;现在我想在textview中打印它。我写了下面的代码

 num1 =(TextView)findViewById(R.id.textView14); 
num1.setText(a);



当我运行程序时出现错误,说ResourceNotFoundException字符串ID



我可能犯的错误是什么?请引导我完成它

解决方案

setText [ ^ ]带有一个int参数,用于设置来自资源的文本,其中int是资源ID ...在您的情况下,它将查找id为4的资源 - 该资源不存在...

使用 .toString() int类型的方法将数字转换为字符串...


将数字转换为字符串即

int num = 5;

字符串n = num +;



然后,

num1.setText(n);



-KR






该错误表示找不到您使用的资源。



请检查你已经创建了一个 TextView 机智h id textView14



最后,换行:

 num1.setText(a); 



to:

 num1.setText(a.toString()); 



这是因为TextViews只接受String形式的文本。


i am facing a small and silly error which i am not able to find out. in my main class i declared a variable int a=4; now i want to print it in a textview. i wrote the following code

num1=(TextView) findViewById(R.id.textView14);
num1.setText(a);


when i run the program i get an error saying ResourceNotFoundException String ID

what can be the mistake that i have done? please guide me through it

解决方案

setText[^] with a single int parameter used to set the text from a resource where the int is the resource id...In your case it will look for the resource with id 4 - which does not exists...
Convert the number to string using .toString() method of the int type...


Convert the number to string i.e
int num = 5;
String n = num + "";

and then,
num1.setText(n);

-KR


Hi,

The error means a resource you've used couldn't be found.

Please check that you've created a TextView with the id of textView14.

Lastly, change the line:

num1.setText(a);


to:

num1.setText(a.toString());


This is because TextViews only accept text in form of String.


这篇关于如何在android中以编程方式将数字放到textview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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