gpt4 book ai didi

java - 单击按钮后android应用崩溃的原因

转载 作者:行者123 更新时间:2023-12-03 17:38:30 25 4
gpt4 key购买 nike

我正在尝试制作一个创建对象并将其保存在TreeSet中的应用程序,但是当我按下按钮时,该应用程序将崩溃。我需要帮助

MainActivity如下:

public class MainActivity extends Activity {
private TreeSet<Variable> arbreDeVariables = new TreeSet<Variable>();

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
creeUneVariable();
}

private void creeUneVariable() {
Button boutonEnvoyer = (Button)findViewById(R.id.button1);
boutonEnvoyer.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Variable variable = new Variable(getUsername(), getPassword());
arbreDeVariables.add(variable);
}

});
}
private String getUsername(){
final EditText username = (EditText)findViewById(R.id.editText1);
return username.getText().toString();
}
private String getPassword(){
final EditText password = (EditText)findViewById(R.id.editText2);
return password.getText().toString();

}


}

“变量”类如下:
public class Variable {
private String username;
private String password;

public Variable(String username,String password){
this.username = username;
this.password = password;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String toString(){
return "Username: "+username+" Password: "+password;
}

}

最佳答案

我会说您的editText或button中的一个未命名,就像您在代码中使用它一样。.因此,您将获得NullPointerException

关于java - 单击按钮后android应用崩溃的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17123237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com