1

我一直在尝试通过 EditText 接受两个值,1) inpval2 和 2) rate。我已经使用了 Integer 类和 parseInt() 方法以及

android:digits="0123456789." android:inputType="number" 将接受的值转换为整数的属性。

这是代码:

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") 时,应用程序停止工作。

如果我删除

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

单击按钮后应用程序不会崩溃。

以下是 logcat 错误:

> 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

谁能帮我解决这个问题?我是一个初学者,花了很多时间来尝试找到解决方案。

4

3 回答 3

3

这是你的问题:

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 

该行at kk.currency.CurrencyActivity.btnclick(CurrencyActivity.java:24)告诉您,在 CurrencyActivity.java 文件中的第 24 行,由于 int 无效,即空字符串“”,它抛出了 NumberFormatException。这意味着您单击其中一个字段为空,并且它试图将“”解析为整数并在失败时抛出异常。

于 2012-05-16T21:38:20.843 回答
2

您应该发布 logcat 错误,以便我们为您提供更多帮助。

有两件事可能会导致您的崩溃:

1.)您无法将输入解析为整数(超出范围的值?正在解析的文本?十进制值?)

或者

2.) 您的 findViewById 调用返回 null,当您调用inpval1.getText().toString()它时抛出 NullPointerException

于 2012-05-16T21:28:16.550 回答
1
String s1 = inpval1.getText().toString();
String s2 = inpval2.getText().toString();
String s3 = rate.getText().toString();

int v1=Integer.parseInt(s1);
int v2=Integer.parseInt(s2);
int r = Integer.parseInt(s3);
于 2012-05-16T21:54:40.897 回答