在一个内部类最后一个变量的问题 [英] final variable issue in an inner class

查看:131
本文介绍了在一个内部类最后一个变量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能告诉我什么是错误吗?它告诉我,在举杯使性别必须设置为最后的,但是当我做什么,if语句抱怨说,它不应该是最后的

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){    字符串性别= NULL;    Toast.makeText(这一点,R.string.ok,Toast.LENGTH_LONG).show();
super.onCreate(savedInstanceState);
的setContentView(R.layout.createprofile);    单选rb_male =(单选)findViewById(R.id.maleradiobutton);
。字符串男性= rb_male.getText()的toString();    单选rb_female =(单选)findViewById(R.id.femaleradiobutton);
    。字符串女= rb_male.getText()的toString();    如果(rb_male.isChecked()){性别=男;}
    如果(rb_female.isChecked()){性别=女;}    按钮checkgender =(按钮)findViewById(R.id.gendercheck);
    checkgender.setOnClickListener(新View.OnClickListener(){        公共无效的onClick(视图v){            Toast.makeText(ProfileActivity.this,gender.toString(),Toast.LENGTH_LONG);
        }
    });
}
}


解决方案

您在那里有太多的错误,真正彻底解决。它们包括:


  1. 您抓住从按钮上的文字转换为变量,那么你写出来的文字字符串男性和女性反正。

  2. 您是霎那从男性按钮上的文字两次,从未阴钮。

  3. 声明变量在类中,而不是在onCreate方法,他们的行为,没有任何问题更好的关于是终局的,等等。

  4. 如果语句检查男性和女性的按钮必须在方法内部的onClick(V)。如果他们在外面,然后点击不会导致他们重新评估并没有改变你的按钮数量将改变你得到的结果。

  5. 性别已经是一个字符串, gender.toString()并没有什么改变。

  6. 单选按钮并不真正做你想做的,你可能想切换按钮。

  7. 您可能想要把一些逻辑所以只有按钮男性或女性可以一次检查之一。他们将各需要一个OnClickListener做到这一点。

说了这么多,这里是code有很多的清理了一下做这些变化,可能有一些其他的东西。

祝你好运!

公共类AndroidTest延伸活动{

 字符串性别= NULL;
   单选rb_male;
   单选rb_female;
   按钮checkgender;   @覆盖
   保护无效的onCreate(捆绑savedInstanceState){       Toast.makeText(这一点,OK,Toast.LENGTH_LONG).show();
       super.onCreate(savedInstanceState);
       的setContentView(R.layout.main);       rb_male =(单选)findViewById(R.id.maleradiobutton);
       rb_female =(单选)findViewById(R.id.femaleradiobutton);       checkgender =(按钮)findViewById(R.id.gendercheck);
       checkgender.setOnClickListener(新View.OnClickListener(){           公共无效的onClick(视图v){
               如果(rb_male.isChecked()){
                   性别=男;
               }
               如果(rb_female.isChecked()){
                   性别=女;
               }
               Toast.makeText(AndroidTest.this,(CharSequence的)性别,Toast.LENGTH_LONG).show();
           }
       });
   }

}

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




}




}

解决方案

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

}

这篇关于在一个内部类最后一个变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆