开发者

android code seems to be giving errors while using switch statment

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;
            }
        }
    };
}


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.


I think the problem is here:

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

Rename this ids without "-".


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

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

etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜