登录方法有问题。我的活动包含片段容器和 2 个按钮(登录和注册)。在活动的创建方法中,我初始化片段,并将登录片段添加到容器中。
在 onClickSignButtons 方法中,我有 switch case 函数,它应该获取按下的按钮 id 并根据按钮 id 进行一些思考。
但点击按钮应用程序崩溃。
logcat 的日志如下:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.enjoyapp.eventmanagement.Screens.Login.View.LoginActivity.onClickSignButtons(LoginActivity.java:45)
登录活动
public class LoginActivity extends AppCompatActivity implements LoginModel {
private LoginPresenter presenter;
private Fragment EmailAndPasswordFragment, userDetailsFragment;
private Fragment selectedFragment = null;
UserSingleton userSingleton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
presenter = new LoginPresenter(this);
EmailAndPasswordFragment = new EmailAndPasswordFieldsFragment();
userDetailsFragment = new UserDetailsFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fields_container, new EmailAndPasswordFieldsFragment())
.commit();
selectedFragment = EmailAndPasswordFragment;
}
public void onClickSignButtons(View view) {
switch (view.getId()) {
case R.id.btnSignIn:
if (selectedFragment == EmailAndPasswordFragment) {
userSingleton.setmEmail(((EditText) EmailAndPasswordFragment.getView().findViewById(R.id.etEmail)).getText().toString().trim());
userSingleton.setmPassword(((EditText) EmailAndPasswordFragment.getView().findViewById(R.id.etPassword)).getText().toString().trim());
presenter.signIn(userSingleton.getmEmail(), userSingleton.getmPassword());
} else {
getSupportFragmentManager().beginTransaction().add(R.id.fields_container, new EmailAndPasswordFieldsFragment())
.commit();
selectedFragment = EmailAndPasswordFragment;
桃花长相依
慕的地6264312
慕姐4208626
30秒到达战场
相关分类