开发者

Android: Button onclick showing error

开发者 https://www.devze.com 2023-03-14 14:01 出处:网络
For my two buttons I want to implement a switch case and with on click I am getting the following errors how to solve this?

For my two buttons I want to implement a switch case and with on click I am getting the following errors how to solve this?

void is an invalid type for the variable onClick Syntax error on token ")", ; expected

Here is my code.

        Button regmalebtn = (Button) findViewById(R.id.regmalebtn);
        Button regfemalebtn = (Button) findViewById(R.id.regfemalebtn);

    String gender; 
    gender = regmalebtn.getText().toString();
  public void onClick(View v) {

      switch(v.getId()){

      case R.id.regmalebtn:
    //    String gender; 
    //    gender = regmalebtn.getText().toString();
        gender.equals("M");
    //  request.addProperty("g开发者_开发技巧ender",gender );

           break;

      case R.id.regfemalebtn:
          gender = regfemalebtn.getText().toString();
            gender.equals("F");
            //request.addProperty("gender", gender);
           break;
           default:
               break;
      }
  }  


You are getting the following error

Syntax error on token ")", ; expected

That means onClick() method is defined inside another method. It should be defined outside of any method in the same class

Thanks Deepak


Please remove default: break;

from case R.id.regfemalebtn:

And code must looks like this:

public void onClick(View v) {

      switch(v.getId()){

      case R.id.regmalebtn:
    //    String gender; 
    //    gender = regmalebtn.getText().toString();
        gender.equals("M");
    //  request.addProperty("gender",gender );

           break;

      case R.id.regfemalebtn:
          gender = regfemalebtn.getText().toString();
            gender.equals("F");
            //request.addProperty("gender", gender);
           break;
      }
default:
               break;
  } 

Hope it helps you


  public class Sample extends Activity implements OnClickListener
   {
      public void onCreate(Bundle bundle)
       {
           super.onCreate(bundle);
           setContentView(//ur layout);
           Button regmalebtn = (Button) findViewById(R.id.regmalebtn);
           Button regfemalebtn = (Button) findViewById(R.id.regfemalebtn);
           regmalebtn.setOnClickListener(this);
           regfemalebtn.setOnClickListener(this);
           String gender; 
           gender = regmalebtn.getText().toString();
       }//onCreate()
       public void onClick(View v) {
          switch(v.getId()){
               case R.id.regmalebtn:
                       //String gender; 
                       //    gender = regmalebtn.getText().toString();
                       gender.equals("M");
                        //  request.addProperty("gender",gender );
                      break;
               case R.id.regfemalebtn:
                       gender = regfemalebtn.getText().toString();
                       gender.equals("F");
                       //request.addProperty("gender", gender);
                      break;
               default:
                      break;
      }//onclick()
 }//Sample  

Try this code..

0

精彩评论

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