0

The application is crashing when pressing the Computecost button

package com.example.hw_3;

import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
import android.content.*;

public class ShoppingExpensesPage extends Activity  

{
    TextView et;
    Button computecost;
    Button save;
    Button cancel;
    RadioGroup drinks;
    RadioButton drink;
    int tottalcost=0;

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        et=(TextView)findViewById(R.id.tv11);
          Bundle extra = getIntent().getExtras();
          String val1 = extra.getString("value");
          et.setText(val1);
          computecost=(Button)findViewById(R.id.btn11);
          computecost.setOnClickListener(new View.OnClickListener() 
          {
            @Override
            public void onClick(View view) 
            {
                String val;

                int selectedId = drinks.getCheckedRadioButtonId();
                drink = (RadioButton)findViewById(selectedId);
                val=(String) drink.getText();
                if(val=="Juice")
                {tottalcost=tottalcost+3;

                }
                else if (val=="Cola")
                {

                    tottalcost=tottalcost+2;

                }

                et.setText(Integer.toString(tottalcost));
            }
          });

          save=(Button)findViewById(R.id.btn21);
          save.setOnClickListener(new View.OnClickListener()
          {
            @Override

            public void onClick(View v) {
                Intent returnIntent = new Intent();
                 String  str=Integer.toString(tottalcost);//(String) et.getText();
                returnIntent.putExtra("return", str);
                 setResult(RESULT_OK,returnIntent);     
                 finish();                  
            }
          });
    }
}
nhgrif
  • 61,578
  • 25
  • 134
  • 173
HSN
  • 783
  • 3
  • 11
  • 20

2 Answers2

4

You haven't set the view for drinks.

int selectedId = drinks.getCheckedRadioButtonId();

Find a view for it, before computecost.setOnClickListener:

drinks = (RadioGroup) findViewById(...);
Cloudream
  • 631
  • 6
  • 14
1

This is the wrong way to compare Strings

if(val=="Juice")

In Java, "==" compares the reference of the Objects but not their values. You need to use .equals()

 if("Juice".equals(val))
 {   // do something  }

If this doesn't solve your problem then please post the logcat from the error but this still needs to be changed.

codeMagic
  • 44,549
  • 13
  • 77
  • 93