我制作两个片段,其中一个登录另一个是注册,在播放我的应用程序时我更改片段注册我的应用程序显示错误,在注册片段我将setOnclickListner分配给一个按钮,我的应用程序显示错误线。
setOnClickListener出错:
显示java.lang.NullPointerException
MainActivity Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getFragmentManager();
transaction = manager.beginTransaction();
login = new FragmentLogin();
register = new FragmentRegister();
transaction.add(R.id.fragment_container, login);
transaction.commit();
LoginPage = true;
txtChangePage = (TextView) findViewById(R.id.txtLoginRegister);
txtChangePage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ChangePage();
}
});
}
public void ChangePage() {
transaction2 = manager.beginTransaction();
if (LoginPage) {
transaction2.replace(R.id.fragment_container, register);
txtChangePage.setText("Aleardy a member? Login");
LoginPage = false;
registerBtn= (Button) findViewById(R.id.btnRegister);
registerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText userName= (EditText) findViewById(R.id.UsernameRegister);
EditText password= (EditText) findViewById(R.id.PasswordRegister);
EditText confirmPassword= (EditText) findViewById(R.id.confirmPasswordRegister);
DataModel userData=new DataModel(MainActivity.this);
userData.addUser(userName.getText().toString(),password.getText().toString());
});
} else {
transaction2.replace(R.id.fragment_container, login);
txtChangePage.setText("No account yet? Create One");
LoginPage = true;
}
transaction2.commit();
}
我很感激帮助我理解出了什么问题