无法接受通过的EditText整数值 [英] Unable to accept integer value through EditText

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

问题描述

我一直在试图通过的EditText接受两个值,1)inpval2和2)率。我已经使用Integer类和parseInt函数()方法沿

I have been trying to accept two values, 1) inpval2 and 2) rate, through EditText. I have used the Integer class and parseInt() method along with

安卓位数=0123456789。
 安卓的inputType =号

 属性接受的值转换为整数。

android:digits="0123456789." android:inputType="number" attributes to convert accepted values as integers.

下面是code:

package kk.currency;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class CurrencyActivity extends Activity {
/** Called when the activity is first created. */

EditText inpval1;
EditText inpval2;
EditText rate;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void btnclick(View view)  {

        inpval1 = (EditText)findViewById(R.id.away);
        int v1 = Integer.parseInt(inpval1.getText().toString());

        inpval2 = (EditText)findViewById(R.id.home);
        int v2 = Integer.parseInt(inpval2.getText().toString());

        rate = (EditText)findViewById(R.id.rate);
        int r = Integer.parseInt(rate.getText().toString());  

        v1 = v2 * r;

    }

}

现在的问题是,当我按一下按钮(的onClick =btnclick)的应用程序停止工作。

Now the problem is that when I click the button (onClick = "btnclick") the app stops working.

如果我删除

int v1 = Integer.parseInt(inpval1.getText().toString());
int v2 = Integer.parseInt(inpval2.getText().toString());
int r = Integer.parseInt(rate.getText().toString());

应用程序不经点击按钮崩溃。

the app does not crash upon the click of the button.

下面是logcat的错误:

Here are the logcat errors:

> 05-17 02:59:04.676: D/gralloc_goldfish(869): Emulator without GPU emulation detected.
05-17 02:59:13.907: D/AndroidRuntime(869): Shutting down VM
05-17 02:59:13.907: W/dalvikvm(869): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-17 02:59:13.946: E/AndroidRuntime(869): FATAL EXCEPTION: main
05-17 02:59:13.946: E/AndroidRuntime(869): java.lang.IllegalStateException: Could not execute method of the activity
05-17 02:59:13.946: E/AndroidRuntime(869):  at android.view.View$1.onClick(View.java:3044)
05-17 02:59:13.946: E/AndroidRuntime(869):  at android.view.View.performClick(View.java:3511)
05-17 02:59:13.946: E/AndroidRuntime(869):  at android.view.View$PerformClick.run(View.java:14105)
05-17 02:59:13.946: E/AndroidRuntime(869):  at android.os.Handler.handleCallback(Handler.java:605)
05-17 02:59:13.946: E/AndroidRuntime(869):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-17 02:59:13.946: E/AndroidRuntime(869):  at android.os.Looper.loop(Looper.java:137)
05-17 02:59:13.946: E/AndroidRuntime(869):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-17 02:59:13.946: E/AndroidRuntime(869):  at java.lang.reflect.Method.invokeNative(Native Method)
05-17 02:59:13.946: E/AndroidRuntime(869):  at java.lang.reflect.Method.invoke(Method.java:511)
05-17 02:59:13.946: E/AndroidRuntime(869):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-17 02:59:13.946: E/AndroidRuntime(869):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-17 02:59:13.946: E/AndroidRuntime(869):  at dalvik.system.NativeStart.main(Native Method)
05-17 02:59:13.946: E/AndroidRuntime(869): Caused by: java.lang.reflect.InvocationTargetException
05-17 02:59:13.946: E/AndroidRuntime(869):  at java.lang.reflect.Method.invokeNative(Native Method)
05-17 02:59:13.946: E/AndroidRuntime(869):  at java.lang.reflect.Method.invoke(Method.java:511)
05-17 02:59:13.946: E/AndroidRuntime(869):  at android.view.View$1.onClick(View.java:3039)
05-17 02:59:13.946: E/AndroidRuntime(869):  ... 11 more
05-17 02:59:13.946: E/AndroidRuntime(869): Caused by: java.lang.NumberFormatException: Invalid int: ""
05-17 02:59:13.946: E/AndroidRuntime(869):  at java.lang.Integer.invalidInt(Integer.java:138)
05-17 02:59:13.946: E/AndroidRuntime(869):  at java.lang.Integer.parseInt(Integer.java:359)
05-17 02:59:13.946: E/AndroidRuntime(869):  at java.lang.Integer.parseInt(Integer.java:332)
05-17 02:59:13.946: E/AndroidRuntime(869):  at kk.currency.CurrencyActivity.btnclick(CurrencyActivity.java:24)
05-17 02:59:13.946: E/AndroidRuntime(869):  ... 14 more

任何人都可以请帮我这个?我是一个初学者,已经花了相当多的时间在这,试图找到一个解决方案。

Can anyone please help me with this? I'm a beginner and have spent quite a lot of time on this to try find a solution.

推荐答案

下面是你的问题:

05-17 02:59:13.946: E/AndroidRuntime(869): Caused by: java.lang.NumberFormatException: Invalid int: "" 
05-17 02:59:13.946: E/AndroidRuntime(869):  at java.lang.Integer.invalidInt(Integer.java:138) 
05-17 02:59:13.946: E/AndroidRuntime(869):  at java.lang.Integer.parseInt(Integer.java:359) 
05-17 02:59:13.946: E/AndroidRuntime(869):  at java.lang.Integer.parseInt(Integer.java:332) 
05-17 02:59:13.946: E/AndroidRuntime(869):  at kk.currency.CurrencyActivity.btnclick(CurrencyActivity.java:24) 
05-17 02:59:13.946: E/AndroidRuntime(869):  ... 14 more 

在kk.currency.CurrencyActivity.btnclick(CurrencyActivity.java:24)行告诉你,在你的CurrencyActivity.java文件,在24行,它抛出一个因为无效int,它是空字符串的NumberFormatException异常。这意味着你与你的字段为空的一个点击,并且它试图解析,作为一个整数,当它失败时抛出一个异常。

The line at kk.currency.CurrencyActivity.btnclick(CurrencyActivity.java:24) tells you that in your CurrencyActivity.java file, on line 24, it's throwing a NumberFormatException because of an invalid int, which is the empty String "". This means you're clicking with one of your fields empty, and it's trying to parse "" as an integer and throwing an exception when it fails.

这篇关于无法接受通过的EditText整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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