-1

i was trying to use switch statement for the radiobutton but i am etting errors when i run it. can somebody please help me out. i have pasted my android code below. please help if you know the answer. thanks a lot.

package apt.tutorial;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.R;

public class LunchtimeActivity extends Activity {

    Restaurant r=new Restaurant();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button save=(Button)findViewById(R.id.button1);
        save.setOnClickListener(onSave);
    }

    private View.OnClickListener onSave=new View.OnClickListener() {
        public void onClick(View v) {
            EditText name=(EditText)findViewById(R.id.text1);
            EditText address=(EditText)findViewById(R.id.text2);

            r.setName(name.getText().toString());
            r.setAddress(address.getText().toString());

            RadioGroup types=(RadioGroup)findViewById(R.id.types);

            switch (types.GetCheckedRadiobuttonId()) {
                case R.id.Sit-Down:
                    r.setType("Sit_Down");
                    break;
                case R.id.Take-Out:
                    r.setType("Take_Out");
                    break;
                case R.id.Delivery:
                    r.setType("Delivery");
                    break;
            }
        }
    };
}
1
  • Pls add the error stack trace in your question. Commented Oct 28, 2023 at 13:17

3 Answers 3

1

I can see following problems:

  1. You've imported android.R class which clearly doesn't have these fields (e.g. Sit-Down etc
  2. You're calling a method of RadioGroup which doesn't exists change from GetCheckedRadiobuttonId to getCheckedRadioButtonId
  3. Id's can only contain characters that allowed in Java variable names so Sit-Down is not valid indentifier.
Sign up to request clarification or add additional context in comments.

Comments

0

I think the problem is here:

case R.id.Sit-Down:
case R.id.Take-Out:

Rename this ids without "-".

Comments

0

You need to capture the radio buttons in the view aswell as the radio group

RadioButton sitdown = (RadioButton) findViewByid(R.id.Sit-Down);

etc.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.