问题
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:
- You grab text from the button into variables, then you write out literal strings "male" and "female" anyway.
- You were grabbing text from the male button twice, and never the female button.
- 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.
- Your
ifstatements 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. genderis already a String,gender.toString()does nothing to change that.- RadioButton don't really do what you want, you probably want ToggleButton.
- 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