-4

I want to compare two Spinner values and, according to them, I want to display a result.
I have tried the following code, but everytime I run the application it does not show the expected result.

How to compare those values?

Here is the code

public class MyApp extends Activity implements OnItemSelectedListener {

    Spinner temp1,temp2;
    TextView t1,t2,t3;
    EditText itemp;
    Integer i,o,conv;
    Double tc,tk,tf;
    String sel1,sel2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);

        t1=(TextView)findViewById(R.id.t1);
        t2=(TextView)findViewById(R.id.t2);
        t3=(TextView)findViewById(R.id.otemp);
        itemp=(EditText)findViewById(R.id.itemp);

        temp1=(Spinner)findViewById(R.id.temp);
        ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.temperature,android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        temp1.setAdapter(adapter);
        temp1.setOnItemSelectedListener(this);

        temp2=(Spinner)findViewById(R.id.temp2);
        ArrayAdapter<CharSequence> adapter2=ArrayAdapter.createFromResource(this,R.array.temperature,android.R.layout.simple_spinner_item);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        temp2.setAdapter(adapter2);
        temp2.setOnItemSelectedListener(this);

        tk=Double.parseDouble(itemp.getText().toString());
        if (sel1.equals("MyappVer") && sel2.equals("MYAPPVER")){
        t3.setText("="+tk);
        }

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // On selecting a spinner item
        Spinner temp1=(Spinner)parent;
        Spinner temp2=(Spinner)parent;
        if (temp1.getId()==R.id.temp) {
            String item1 = parent.getItemAtPosition(position).toString();

            t1.setText(item1);
            sel1=item1;
        }
        if (temp2.getId()==R.id.temp2) {
            String item2 = parent.getItemAtPosition(position).toString();

            t2.setText(item2);
            sel2=item2;
        }

    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

}

When I remove these lines the app works, but I need to compare those Spinner values

if (sel1.equals("MyappVer") && sel2.equals("MYAPPVER")){
            t3.setText("="+tk);
        }

Is something wrong with this implementation?

Logcat

08-04 14:48:28.651  13541-13541/com.combud.calcthree.compencalculator     E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.combud.calcthree.compencalculator/com.combud.calcthree.compenc alculator.MyApp}: java.lang.NumberFormatException: Invalid double: ""
            at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
            at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
            at android.app.ActivityThread.access$600(ActivityThread.java:127)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4507)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NumberFormatException: Invalid double: ""
            at java.lang.StringToReal.invalidReal(StringToReal.java:63)
            at java.lang.StringToReal.parseDouble(StringToReal.java:248)
            at java.lang.Double.parseDouble(Double.java:295)
            at     com.combud.calcthree.compencalculator.MyApp.onCreate(MyApp.java:50)
            at android.app.Activity.performCreate(Activity.java:4469)
            at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
            at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
            at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
            at     android.app.ActivityThread.access$600(ActivityThread.java:127)
            at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
            at android.os.Handler.dispatchMessage(Handler.java:99)
Saurabh
  • 62
  • 1
  • 15
  • possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – 2Dee Aug 04 '15 at 08:28
  • "When I remove these lines the app works" what happens if you keep these lines? Please also read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – Sufian Aug 04 '15 at 09:26
  • possible duplicate of [java.lang.numberformatexception: invalid double: " "](http://stackoverflow.com/questions/18672456/java-lang-numberformatexception-invalid-double) – Sufian Aug 04 '15 at 10:38

2 Answers2

2
String sel1= temp1.getSelectedItem().toString();
String sel2= temp2.getSelectedItem().toString();
if (sel1.equals("MyappVer") && sel2.equals("MYAPPVER")){
            t3.setText("="+tk);
        }

update your code with the below one. and what you want to do in your application tell me your requirement there is no meaning of writing you if statement there

Bala Saikrupa Puram
  • 721
  • 1
  • 7
  • 28
1

This is due to your sel1 and sel2 string are null at the time of

 if (sel1.equals("MyappVer") && sel2.equals("MYAPPVER")){
        t3.setText("="+tk);
        }

and you are trying to check the value on null object

Do like this

String sel1 = "",sel2 = "";

Also add the value of them before checking them like

sel1= temp1.getSelectedItem().toString();
sel2= temp2.getSelectedItem().toString();
if (sel1.equals("MyappVer") && sel2.equals("MYAPPVER")){
            t3.setText("="+tk);
        }

If you want to update these value after selection of spinner add above lines in your onItemSelected method of spinner not in onCreate method

Let me know if this helps

Edit:

According to your logcat exception is due to

Caused by: java.lang.NumberFormatException: Invalid double: ""

at line

tk=Double.parseDouble(itemp.getText().toString());

This is due to the edit text itemp is not having any value and still you are accessing data and parsing it to double which is causing that exception.

So before parsing value check the value of edit text like

String textValue = itemp.getText().toString();
  if(textValue != null && !textValue.equalIgnoreCase("")){
  tk=Double.parseDouble(textValue);
}
Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51