android编辑文本值作为参数直接传递给对象函数或构造函数不起作用

时间:2015-05-09 19:04:20

标签: android android-edittext

以下是课程代码:

public class UserInfo 
{
    String userName;
    String userPass;

    public UserInfo(){

    }
    public UserInfo(String userName, String userPass){
        userName=this.userName;
        userPass=this.userPass;
    }
    public void setUserName(String userName){
        userName=this.userName;
    }
    public void setUserPass(String userPass){
         userPass=this.userPass;
    }
}

如果我将字符串传递给来自以下活动的对象函数:userInfo.setUserName(et_login_user_name.getText().toString());

然后userName未设置为编辑框的值。它被设置为null。

但如果我将值设置如下: userInfo.userName=et_login_user_name.getText().toString();

然后代码正常工作。

以下是活动类代码:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    final Context ctx=this;

    final Button bt_login=(Button)findViewById(R.id.button5);
    bt_login.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            UserInfo userInfo = new UserInfo();
            EditText et_login_user_name=(EditText)findViewById(R.id.editText);
            EditText et_login_user_pass=(EditText)findViewById(R.id.editText2);
//   userInfo.setUserName(et_login_user_name.getText().toString()); -not working 
//   userInfo.setUserPass(et_login_user_pass.getText().toString()); -not working

            userInfo.userName=et_login_user_name.getText().toString();  // working
            userInfo.userPass=et_login_user_pass.getText().toString();  // working
            if(userInfo.userPass.isEmpty()||userInfo.userName.isEmpty()) //get null pointer exception for non working code
            {
              //some code
            }

3 个答案:

答案 0 :(得分:2)

您正在错误地分配值,而不是将值分配给类成员,而是将类成员值分配给传递的参数。即代替 userName = this.userName使用

this.userName = userName;

答案 1 :(得分:0)

使用此选项可以从类变量为本地函数变量赋值。下面的代码现在适合你

public class UserInfo 
{
    String userName;
    String userPass;

    public UserInfo(){

    }
    public UserInfo(String userName, String userPass){
        userName=this.userName;
        userPass=this.userPass;
    }
    public void setUserName(String userName){
        this.userName=userName;
    }
    public void setUserPass(String userPass){
         this.userPass=userPass;
    }
}

答案 2 :(得分:0)

this.userName指向该类的局部变量userName而不是接收到的参数。

最初这些都是空的,

String userName;
String userPass;

这是你犯错的地方。

 public void setUserName(String userName){
        userName=this.userName; // assume userName u passed was admin, then, admin = userName where userName is empty which causes null pointer exception
    }

所以,它应该是

public void setUserName(String userName){
        this.userName=userName; //in this case userName = admin;
    }

此外,您需要为该类中的整个事物更改相同的分配内容。所以,你的最终代码应该是

public class UserInfo 
{
    String userName;
    String userPass;

    public UserInfo(){

    }
    public UserInfo(String userName, String userPass){
        this.userName= userName;
        this.userPass =  userPass;
    }
    public void setUserName(String userName){
        this.userName= userName;
    }
    public void setUserPass(String userPass){
        this.userPass =  userPass;
    }
}