parseInt 崩溃 Android 应用程序
所以我正在尝试制作一个计算器应用程序(只是为了掌握android开发的窍门),并且我注意到通过一些测试,从“String tit1”的parseInt转换导致应用程序崩溃。有人能告诉我为什么吗?我似乎无法弄清楚,而且我已经在互联网上搜索了很长一段时间。 (呵呵,我是菜鸟,所以请放轻松)在下面的代码中,我修改了几行,使其看起来应该打印的内容很明显,但它仍然崩溃。这是代码:
equals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
oC = 1; //operator checker is on 1 (for plus)
switch(oC){ //check for operator clicked
case 0: break;
case 1:
tack1 = "1"; //INSERTED THIS TO MAKE OUTPUT OBVIOUS
tack1 = tack1.trim(); tack2 = tack2.trim(); //INSERTED BEFORE TO DEAL WITH WHITESPACE
numOne = Integer.parseInt(tack1); //CRASHES HERE
answer.setText(numOne);
modeChecker = 0; oC = 0;break;
程序注释(一些重复的注释和其他内容):
tack1 =“1”;是使输出明显
tack1.trim()是处理空格
是的,tack中的任何内容都是数字和整数(甚至不是负整数)
是的numOne是一个整数,并且在上面定义为wayy(不在此处列出的代码中) )
抱歉,由于我添加的注释,缩进全部混乱(在情况 1 之后)。
这是我的 onClick 方法的一部分,因此此处不包含右括号。
有人可以帮我吗? 谢谢你:D
So I'm trying to make a calculator app (just to get a hang of android development), and I noticed with some testing that the parseInt conversion from "String tack1" is causing the app to crash. Can someone tell me why? I can't seem to figure it out and I've been searching the internet for quite a while now. (hehe I'm a noob so please go easy) In the code below, I've modified a couple lines to make it seem obvious what it's supposed to print however it still crashes. Here's the code:
equals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
oC = 1; //operator checker is on 1 (for plus)
switch(oC){ //check for operator clicked
case 0: break;
case 1:
tack1 = "1"; //INSERTED THIS TO MAKE OUTPUT OBVIOUS
tack1 = tack1.trim(); tack2 = tack2.trim(); //INSERTED BEFORE TO DEAL WITH WHITESPACE
numOne = Integer.parseInt(tack1); //CRASHES HERE
answer.setText(numOne);
modeChecker = 0; oC = 0;break;
NOTES ON PROGRAM(some of comments repeated and other stuff as well):
The tack1 = "1"; is to make output obvious
The tack1.trim() is to deal with whitespace
Yes whatever is in tack is a number and an integer (not even a negative integer)
Yes numOne is an integer and is defined wayy above (not in code listed here)
Sorry the indents are all messed up(after case 1) because of the comments I added
This is a section of my onClick method, so closing brackets aren't included in here.
Can someone please help me?
THANKYOU :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我敢打赌,在调用
parseInt
之后,它实际上会在下一行崩溃。您正在 TextView 上调用
setText(int)
。当您将 int 传递给此方法时,该 int 是指向字符串资源的指针...您可能期望自动转换为字符串。由于您向其传递了应用程序中生成的 int,因此该 int 不太可能也指向 res/values/strings.xml 文件中的字符串资源。您真正想做的是首先将 numOne 更改为字符串,您可以内联执行此操作:更改
为
,然后就可以开始了。
I'd be willing to bet it's actually crashing on the following line AFTER the call to
parseInt
.You're calling
setText(int)
on your TextView. When you pass an int to this method, that int is a pointer to a string resource...you're probably expecting an auto-conversion to a string. Since you are passing it an int that is generated in your application, it's extremely unlikely that this int also points to a string resource in your res/values/strings.xml file. What you really want to do is change numOne to a string first, which you can do inline:Change
to
and you're good to go.