final variable issue in an inner class

血红的双手。 提交于 2019-12-11 05:10:07

问题


Could you please tell me what is the error here? it tells me that "gender" in toast make must be set to final but when I do, the if statements complain that it should not be final

@Override
protected void onCreate(Bundle savedInstanceState) {

    String gender = null;

    Toast.makeText(this, R.string.ok, Toast.LENGTH_LONG).show();
super.onCreate(savedInstanceState);
setContentView(R.layout.createprofile);



    RadioButton rb_male = (RadioButton) findViewById(R.id.maleradiobutton);
String   male=rb_male.getText().toString();

    RadioButton rb_female = (RadioButton) findViewById(R.id.femaleradiobutton);
    String female=rb_male.getText().toString();

    if(rb_male.isChecked()) { gender = "male";}
    if(rb_female.isChecked()){ gender = "female";}

    Button checkgender = (Button) findViewById(R.id.gendercheck);
    checkgender.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Toast.makeText(ProfileActivity.this, gender.toString(), Toast.LENGTH_LONG);
        }
    });




}




}

回答1:


You have too many errors in there to really address completely. They include:

  1. You grab text from the button into variables, then you write out literal strings "male" and "female" anyway.
  2. You were grabbing text from the male button twice, and never the female button.
  3. Declare your variables in the class, not in the onCreate method, and they will behave much better with no problems about being final and so on.
  4. Your if statements checking the male and female buttons must be inside the method onClick(v). If they are outside, then a click does not cause them to be reevaluated and no amount of changing your buttons will change the result you get.
  5. gender is already a String, gender.toString() does nothing to change that.
  6. RadioButton don't really do what you want, you probably want ToggleButton.
  7. You probably want to put some logic in so only one of the buttons male or female can be checked at a time. They would each need a OnClickListener to do this.

Having said all that, here is the code with a lot of those changes made and probably some other stuff cleaned up a bit.

Good luck!

public class AndroidTest extends Activity {

   String gender = null;
   RadioButton rb_male;
   RadioButton rb_female;
   Button checkgender;

   @Override
   protected void onCreate(Bundle savedInstanceState) {

       Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       rb_male = (RadioButton) findViewById(R.id.maleradiobutton);
       rb_female = (RadioButton) findViewById(R.id.femaleradiobutton);

       checkgender = (Button) findViewById(R.id.gendercheck);
       checkgender.setOnClickListener(new View.OnClickListener() {

           public void onClick(View v) {
               if (rb_male.isChecked()) {
                   gender = "male";
               }
               if (rb_female.isChecked()) {
                   gender = "female";
               }
               Toast.makeText(AndroidTest.this, (CharSequence) gender, Toast.LENGTH_LONG).show();
           }
       });
   }

}




回答2:


You can't access a local non final variable from an inner class. One easy workaround is to take another final variable after setting the initial value for gender. Do like this:

if(rb_male.isChecked()) { gender = "male";}
if(rb_female.isChecked()){ gender = "female";}
final String finalGender = gender;

and later

Toast.makeText(ProfileActivity.this, finalGender.toString(), Toast.LENGTH_LONG).show();

There are also other ways of doing it but this is the fastest.




回答3:


The error is:

Toast.makeText(ProfileActivity.this, gender.toString(), Toast.LENGTH_LONG);

should be

Toast.makeText(v.getContext(), gender.toString(), Toast.LENGTH_LONG).show();

And you can also make gender a class scope variable so that you can access it with a setter/getter




回答4:


your are missing .show() in onclick toast:

      public void onClick(View v) {

                    Toast.makeText(ProfileActivity.this, gender.toString(), 
Toast.LENGTH_LONG).show();
                }
            });


来源:https://stackoverflow.com/questions/10236446/final-variable-issue-in-an-inner-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!