我有activity_password_change.xml和PasswordChangeActivity。看起来像这样:
public class PasswordChangeActivity extends BaseActivity {
private AutoCompleteTextView mNiu;
private EditText mPassword;
private TextView mCurrentPassword;
private TextView mNewPassword;
private TextView mRepeatPassword;
private Button mChange;
private PasswordChangeViewModel mPasswordChangeViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_password_change);
mNiu = (AutoCompleteTextView) findViewById(R.id.NIU);
mPassword = (EditText) findViewById(R.id.password);
mCurrentPassword = (TextView) findViewById(R.id.current_password);
mNewPassword = (TextView) findViewById(R.id.new_password);
mRepeatPassword = (TextView) findViewById(R.id.repeat_password);
mChange = (Button) findViewById(R.id.Change);
mPasswordChangeViewModel = ViewModelProviders.of(this).get(PasswordChangeViewModel.class);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
mChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mPasswordChangeViewModel.changePassword(mNiu, mPassword, mCurrentPassword, mNewPassword, mRepeatPassword);
}
});
}
....
void changePassword(AutoCompleteTextView niu, EditText password, TextView currentPassword, TextView newPassword, TextView repeatPassword){
if(password.getText().toString().equals(currentPassword.getText().toString()) &&
newPassword.getText().toString().equals(repeatPassword.getText().toString())){
int yourNiu = Integer.parseInt(niu.getText().toString());
if (yourNiu < 10) {
mRepository.changePassword(yourNiu, newPassword.getText().toString());
} else if(yourNiu >= 10 && yourNiu < 20) {
}else{
}
}
}
,我使用来自其他.xml文件(activity_login.xml)的R.id.NIU和R.id.password。我的问题是password.getText()。toString()为null。有没有简单的解决方案?
答案 0 :(得分:0)
,我使用来自其他.xml文件的R.id.NIU和R.id.password (activity_login.xml)
这是您的问题所在。只需在activity_password_change.xml中创建此视图
P.S。您不能使用另一视图层次结构中一个视图层次结构中的视图。
UPD
我假设您正在从LoginActivity启动PasswordChangeActivity。 将此代码放在LoginActivity中,而不是启动PasswordChangeActivity的代码
Intent intent = new Intent(this, PasswordChangeActivity.class);
intent.putExtra("pass", mPassword.getText.toString());
intent.putExtra("niu", mNiu.getText.toString());
startActivity(intent);
在setContentView()方法之后的PasswordChangeActivity中
String pass = getIntent().getStringExtra("pass");
String niu = getIntent().getStringExtra("niu");
删除该行,因为您不再需要
mNiu = (AutoCompleteTextView) findViewById(R.id.NIU);
mPassword = (EditText) findViewById(R.id.password);
将changePassword()签名更改为
void changePassword(String niu, String password, String currentPassword, String newPassword, String repeatPassword)
P.P.S。不要将视图传递给ViewModel,仅将数据传递给ViewModel