How to get sprinner value as integer
android android-widget
Spinner monthSpinner = (Spinner) findViewById(R.id.expMonth);
String selectedmonth=(String)(monthSpinner.getSelectedItem());
int m=Integer.parseInt(selectedmonth.toString());//i am getting error at this statement
System.out.println("Exp-Month"+m);
07-15 11:11:50.568: ERROR/AndroidRuntime(309): java.lang.IllegalStateException: Could not execute method of the activi开发者_JS百科ty 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at android.view.View$1.onClick(View.java:2072) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at android.view.View.performClick(View.java:2408) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at android.view.View$PerformClick.run(View.java:8816) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at android.os.Handler.handleCallback(Handler.java:587) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at android.os.Handler.dispatchMessage(Handler.java:92) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at android.os.Looper.loop(Looper.java:123) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at android.app.ActivityThread.main(ActivityThread.java:4627) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at java.lang.reflect.Method.invokeNative(Native Method) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at java.lang.reflect.Method.invoke(Method.java:521) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at dalvik.system.NativeStart.main(Native Method) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): Caused by: java.lang.reflect.InvocationTargetException 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at com.main.bp.activities.CreditCardActivity.creditCardPayment(CreditCardActivity.java:51) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at java.lang.reflect.Method.invokeNative(Native Method) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at java.lang.reflect.Method.invoke(Method.java:521) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at android.view.View$1.onClick(View.java:2067) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): ... 11 more 07-15 11:11:50.568: ERROR/AndroidRuntime(309): Caused by: java.lang.NumberFormatException: unable to parse 'January' as integer 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at java.lang.Integer.parse(Integer.java:433) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at java.lang.Integer.parseInt(Integer.java:422) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): at java.lang.Integer.parseInt(Integer.java:382) 07-15 11:11:50.568: ERROR/AndroidRuntime(309): ... 15 more
Any one can help me to get spinner selected value as integer
If you are trying to get the position of the selected item, I think you should be using getSelectedItemPosition()
?
(Also in your above code there is no need for the selectedmonth.toString()
as selectedmonth is a String variable.)
精彩评论