Unable to accept integer value via EditText

I am trying to take two values: 1) inpval2 and 2) rate via EditText. I used the Integer method and parseInt () along with

android:digits="0123456789." android:inputType="number" android:digits="0123456789." android:inputType="number" attributes for converting the accepted values ​​as integers.

Here is the 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; } } 

Now the problem is that when the button is pressed (onClick = "btnclick"), the application stops working.

If i remove

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

The application does not crash when a button is clicked.

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 someone help me with this? I am new and spent a lot of time trying to find a solution.

+1
source share
3 answers

Here is your problem:

 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 

The line at kk.currency.CurrencyActivity.btnclick(CurrencyActivity.java:24) tells you that in your CurrencyActivity.java file on line 24, it throws a NumberFormatException because of an invalid int, which is an empty string. "This means that you click one of the fields blank and try to parse "" as an integer and throw an exception when it fails.

+3
source

You should post logcat errors so that we can help you more.

There are two things that can lead to your failure:

1.), you cannot parse your input as an integer (values ​​out of range? Analyzed? Decimal values?)

or

2.) your calls to findViewById return null, and when you call inpval1.getText().toString() , it throws a NullPointerException

+2
source
 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); 
+1
source

All Articles