开发者

Android: Passing a value with the button click using switch case

开发者 https://www.devze.com 2023-03-15 00:59 出处:网络
I have two buttons when we click the first button we should assign Mvalue to a string and for the second button I want to assign F value to the same string variable

I have two buttons when we click the first button we should assign M value to a string and for the second button I want to assign F value to the same string variable

In the below given code gender is the string variable which is null and when we click the regmalebtn we should pass value M to the string gender and F for second button

In the XML I haven't added set-text to both the buttons. when I am doing this, null value is passing and I m getting null pointer exception.

how to solve this? any Help would be greatly appreciated

Thanks in advance...!

    String gender = null; 
Button regmalebtn = (Button) findViewById(R.id.regmalebtn);
  Button regfemalebtn = (Button) findViewById(R.id.regfemalebtn);
     // Here in the above line I m getting the error//
  public void onClick(View v) {

      switch(v.getId()){

      case R.id.regmalebtn:

    //  gender = regmalebtn.getText().toString();
        gender.equals("M");
    //  request.addProperty("gender",gender );

           break;

      case R.id.regfemalebtn: 

       //  gender = regfemalebtn.getText().toString();
            gender.eq开发者_运维百科uals("F");
        //  request.addProperty("gender", gender);
           break;
      default:
          break;

      }
      } 


you have to do like this.

  Button regmalebtn = (Button) findViewById(R.id.regmalebtn);
  regmalebtn.setOnClickListener(this);

  Button regfemalebtn = (Button) findViewById(R.id.regfemalebtn);
  regfemalebtn .setOnClickListener(this);

   public void onClick(View v) 
   {
      switch(v.getId())
      {
          case R.id.regmalebtn:
                               gender = "M";
                               break;

         case R.id.regfemalebtn: 
                              gender = "F";
                              break;

         default:
                              break;
     }
   }


When you tried to use any of these statements:

//  gender = regmalebtn.getText().toString();

or

        gender.equals("F");

both of them are null.

And the correct way to assign a value to a variable is :

variable = value;

In your case, instead of:

gender.equals("F")

you use:

gender = "F"; // or gender = new String("F"); 


 **String gender = "";**  

 Button regmalebtn = (Button) findViewById(R.id.regmalebtn);
  regmalebtn.setOnClickListener(this);

  Button regfemalebtn = (Button) findViewById(R.id.regfemalebtn);
  regfemalebtn .setOnClickListener(this);

   public void onClick(View v) 
   {
      switch(v.getId())
      {
          case R.id.regmalebtn:
                               gender = "M";
                               break;

         case R.id.regfemalebtn: 
                              gender = "F";
                              break;

         default:
                              break;
     }
   }


 public class MainActivity extends AppCompatActivity
 {
   String gen="";
  @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

     final TextView show= (TextView) findViewById(R.id.result);

    //Buttons and their values
        final Button male= (Button) findViewById(R.id.male);
        final Button female= (Button) findViewById(R.id.female);

        final View.OnClickListener gender =new View.OnClickListener(){
            @Override
            public void onClick(View v)
                {
                final int id=v.getId();
                switch (id){
                    case R.id.male:{
                        show.setText("male");   //It will show gender in textView.
                        gen="male";
                        break;
                    }
                    case R.id.female:{
                        show.setText("female");
                        gen="female";
                        break;
                    }
                    default:
                        break;
                }
            }
        };
        male.setOnClickListener(gender);
        female.setOnClickListener(gender);
    }
}
0

精彩评论

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