0

I have two spinners in my app. I want that if i select option "first" from spinner 1 and option "second" from spinner 2, then the action gets performed. But it show "NUMBERFORMATEXCEPTION".

Here's the code

  if (((spinner.getItemAtPosition(pos).toString()=="first" &&        
(s2.getItemAtPosition(id).toString()=="second"))))
   {

       tv.setText(String.valueOf(gmtomilli(x)));
   }

This code has the error, if i omit this code, then the app works fine,without action

  • spinner.getItemAtPosition(pos).toString().equals("first"), only use == when comparing numbers – Carnal Jun 21 '12 at 09:12

1 Answers1

4

Do String comparison using equals.

spinner.getItemAtPosition(pos).toString()=="first"

instead use:

spinner.getItemAtPosition(pos).toString().equals("first")

Similarly for:

s2.getItemAtPosition(id).toString()=="second"

instead use:

s2.getItemAtPosition(id).toString().equals("second")

Read this for more information.

== compares references,not the values. In your case, you want to check for the value equality, not the reference equality.


EDIT:

Since you have mentioned that your code is generating NumberFormatException, I probably believe that either pos or id are of String type generating the NumberFormatException.


EDIT 2:

As per the your comment:

float x=Float.parseFloat(String.valueOf(et.getText())); 

Your getText() is returning a String that can't be actually parsed into a float. Try checking if the content is actually a float in String format. Besides, use String.trim() before parsing to ensure your String doesn't contain any leading or trailing whitespaces that's generating the NumberFormatException.

Community
  • 1
  • 1
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
  • @user1470125 could you please edit your question to provide a few more details,like what type is `pos`,what type is `id`,which exact line is giving you `NumberFormatException`? – Kazekage Gaara Jun 21 '12 at 09:25
  • Both pos and id are globally declared....... public int pos=2; public int id=1; – user1470125 Jun 21 '12 at 09:33
  • @user1470125 you might want to post your entire code, along with the logcat content, because the information you have provided doesn't seem to generate a `NumberFormatException`. – Kazekage Gaara Jun 21 '12 at 09:37
  • The other code is working fine. Made the following changes, still same error. if ((spinner.getSelectedItemPosition()==2) && ((s2.getSelectedItemPosition()==1))) { tv.setText(String.valueOf(gmtomilli(x)+"mg")); – user1470125 Jun 21 '12 at 09:42
  • @user1470125 what is `gmtomilli()` content? what is `x`? If you want better help, kindly post your code. It'll be easier to find the error. Else, I'll have to keep guessing. – Kazekage Gaara Jun 21 '12 at 09:43
  • float x=Float.parseFloat(String.valueOf(et.getText())); gmtomilli is a function – user1470125 Jun 21 '12 at 09:46
  • Yes,thanks the error is in that line only.:-) But i cant get a fix to that. How to check whether that is float in a string format. Kindly help. Thanks – user1470125 Jun 21 '12 at 10:06
  • You need to pass a number to that `EditText`,which you are apparently not doing. – Kazekage Gaara Jun 21 '12 at 10:08
  • Thanks alot!!!! The app worked. But now when i click on the button, the app force closes. The if condition is in the onCreate function. Is that the erro? – user1470125 Jun 21 '12 at 10:21
  • Check the logcat. It will tell you the reason for the force close,and which line is probably causing it. – Kazekage Gaara Jun 21 '12 at 10:27
  • This is the error- 06-21 15:48:46.509: E/AndroidRuntime(2260): java.lang.IllegalStateException: Could not find a method onCreate(View) in the activity class sks.and.ListActivity for onClick handler on view class android.widget.Button with id 'conv' – user1470125 Jun 21 '12 at 10:28
  • I've told you to post your code and your logcat content. Post a new question, as it is a new problem, this question is done as far as the initial `NumerFormatException` problem is concerned. It is really really tough for someone to help without actually looking at your code. – Kazekage Gaara Jun 21 '12 at 10:34
  • Start a new question. Post your code, and your logcat content. This current question has been solved I believe. – Kazekage Gaara Jun 21 '12 at 10:34
  • Oh...sorry. Sure. Will do. Thanks for your help! :) – user1470125 Jun 21 '12 at 10:35
  • And look at this : http://stackoverflow.com/questions/6803127/how-do-i-assign-a-button-to-an-activity-in-java , for the new problem that is coming in your program. I believe there is no `Button` with the id `conv` in your xml. – Kazekage Gaara Jun 21 '12 at 10:36
  • Posted a new question. The button is there. http://stackoverflow.com/questions/11136383/using-to-spinners-app-works-fine-but-when-the-button-is-clicked-error-is-displ – user1470125 Jun 21 '12 at 10:42