点击登录按钮并尝试获取片段 EditText 值时应用程序崩溃

登录方法有问题。我的活动包含片段容器和 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;



炎炎设计
浏览 136回答 4
4回答

桃花长相依

从我所看到的是,您正在初始化片段并将其存储在变量 EmailAndPasswordFragment 中,但是在将片段添加到容器时,您再次重新初始化它。这就是为什么它给出空指针异常。这是你的onCreategetSupportFragmentManager().beginTransaction().add(R.id.fields_container, EmailAndPasswordFragment)         .commit();

慕的地6264312

更新:哦天哪,我很抱歉大家,我只是忘记初始化 UserSingleton 类。刚刚添加到 onCreate 方法的下一行: userSingleton = UserSingleton.getInstance();xml 中有一个新代码public class LoginActivity extends AppCompatActivity implements LoginModel {&nbsp; &nbsp; private LoginPresenter presenter;&nbsp; &nbsp; private Fragment emailAndPasswordFragment;&nbsp; &nbsp; private Fragment userDetailsFragment;&nbsp; &nbsp; private Fragment selectedFragment = null;&nbsp; &nbsp; UserSingleton userSingleton;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_login);&nbsp; &nbsp; &nbsp; &nbsp; presenter = new LoginPresenter(this);&nbsp; &nbsp; &nbsp; &nbsp; emailAndPasswordFragment = new EmailAndPasswordFieldsFragment();&nbsp; &nbsp; &nbsp; &nbsp; userDetailsFragment = new UserDetailsFragment();&nbsp; &nbsp; &nbsp; &nbsp; getSupportFragmentManager().beginTransaction().add(R.id.fields_container, emailAndPasswordFragment)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .commit();&nbsp; &nbsp; &nbsp; &nbsp; selectedFragment = emailAndPasswordFragment;&nbsp; &nbsp; }&nbsp; &nbsp; public void onClickSignInAndUpButtons(View view) {&nbsp; &nbsp; &nbsp; &nbsp; switch (view.getId()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.btnSignIn:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (selectedFragment == emailAndPasswordFragment) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userSingleton.setmEmail(((EditText) emailAndPasswordFragment.getView().findViewById(R.id.etEmail)).getText().toString().trim());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userSingleton.setmPassword(((EditText) emailAndPasswordFragment.getView().findViewById(R.id.etPassword)).getText().toString().trim());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; presenter.signIn(userSingleton.getmEmail(), userSingleton.getmPassword());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getSupportFragmentManager().beginTransaction().add(R.id.fields_container, emailAndPasswordFragment)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .commit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectedFragment = emailAndPasswordFragment;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.btnGetStarted:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //TODO edd signup methos&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }登录活动 XML:<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; xmlns:card_view="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; android:background="@drawable/gradien_color"&nbsp; &nbsp; tools:context=".Screens.Login.View.LoginActivity">&nbsp; &nbsp; <ImageView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/logoMain"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="200dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="200dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_gravity="center"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginStart="8dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="52dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginEnd="8dp"&nbsp; &nbsp; &nbsp; &nbsp; android:contentDescription="@string/logo_main"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintHorizontal_bias="0.497"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintTop_toTopOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:srcCompat="@drawable/logo_main" />&nbsp; &nbsp; <FrameLayout&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/fields_container"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_gravity="center"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginStart="8dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="8dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginBottom="8dp"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintBottom_toTopOf="@+id/cvGetStarted"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintTop_toBottomOf="@+id/logoMain">&nbsp; &nbsp; </FrameLayout>&nbsp; &nbsp; <androidx.cardview.widget.CardView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/cvGetStarted"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="300dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="50dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginStart="8dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginBottom="20dp"&nbsp; &nbsp; &nbsp; &nbsp; android:translationZ="8dp"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintBottom_toTopOf="@+id/btnSignIn"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintHorizontal_bias="0.456"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; card_view:cardCornerRadius="25dp"&nbsp; &nbsp; &nbsp; &nbsp; card_view:cardElevation="3dp">&nbsp; &nbsp; &nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/btnGetStarted"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:background="@drawable/register_button_style"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="@string/btn_get_started"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textColor="@color/white" />&nbsp; &nbsp; </androidx.cardview.widget.CardView>&nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/btnSignIn"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="200dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="50dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginBottom="48dp"&nbsp; &nbsp; &nbsp; &nbsp; android:background="@null"&nbsp; &nbsp; &nbsp; &nbsp; android:onClick="onClickSignInAndUpButtons"&nbsp; &nbsp; &nbsp; &nbsp; android:stateListAnimator="@null"&nbsp; &nbsp; &nbsp; &nbsp; android:text="@string/btn_sign_in"&nbsp; &nbsp; &nbsp; &nbsp; android:textColor="@color/white"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintBottom_toBottomOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintHorizontal_bias="0.497"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>片段类public class EmailAndPasswordFieldsFragment extends Fragment {&nbsp; &nbsp; private EditText etEmail, etPassword;&nbsp; &nbsp; @Override&nbsp; &nbsp; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; View view = inflater.inflate(R.layout.fragment_email_and_password_fields, container, false);&nbsp; &nbsp; &nbsp; &nbsp; etEmail = view.findViewById(R.id.etEmail);&nbsp; &nbsp; &nbsp; &nbsp; etPassword = view.findViewById(R.id.etPassword);&nbsp; &nbsp; &nbsp; &nbsp; return view;&nbsp; &nbsp; }}片段 xml:<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:card_view="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; tools:context=".Screens.Login.View.UserDetailsFragment">&nbsp; &nbsp; <LinearLayout&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="350dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="150dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_gravity="center"&nbsp; &nbsp; &nbsp; &nbsp; android:orientation="vertical"&nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center">&nbsp; &nbsp; &nbsp; &nbsp; <androidx.cardview.widget.CardView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="250dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="40dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginStart="8dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginEnd="8dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginBottom="12dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:translationZ="8dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; card_view:cardBackgroundColor="@android:color/transparent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; card_view:cardCornerRadius="25dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; card_view:cardElevation="3dp">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <EditText&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/etEmail"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="250dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="40dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:background="@drawable/input_field_style"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:drawableStart="@drawable/login_icon"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:drawableTint="@color/tint_and_text_color"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:ems="10"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:hint="@string/hint_email"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:inputType="textEmailAddress"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textColor="@color/main_text_color"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textColorHint="@color/tint_and_text_color"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textSize="16sp" />&nbsp; &nbsp; &nbsp; &nbsp; </androidx.cardview.widget.CardView>&nbsp; &nbsp; &nbsp; &nbsp; <androidx.cardview.widget.CardView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="250dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="40dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginStart="8dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginEnd="8dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginBottom="12dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:translationZ="8dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; card_view:cardBackgroundColor="@android:color/transparent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; card_view:cardCornerRadius="25dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; card_view:cardElevation="3dp">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <EditText&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/etPassword"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="250dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="40dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:background="@drawable/input_field_style"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:drawableStart="@drawable/password_icon"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:drawableTint="@color/tint_and_text_color"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:ems="10"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:hint="@string/hint_password"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:inputType="textPassword"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textColor="@color/main_text_color"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textColorHint="@color/tint_and_text_color"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textSize="16sp" />&nbsp; &nbsp; &nbsp; &nbsp; </androidx.cardview.widget.CardView>&nbsp; &nbsp; </LinearLayout></FrameLayout>

慕姐4208626

EmailAndPasswordFragment&nbsp;=&nbsp;new&nbsp;EmailAndPasswordFieldsFragment();这是 EmailAndPasswordFragment 的第一个实例,在 add 方法中您将传递 EmailAndPasswordFragment 的新实例在 onclick 内部,您正在访问第一个实例的值。

30秒到达战场

你的线路有问题&nbsp;&nbsp;&nbsp;&nbsp;EmailAndPasswordFragment&nbsp;=&nbsp;new&nbsp;EmailAndPasswordFieldsFragment();因为您正在创建 Fragment 对象,但将其分配给类名,而不是变量,然后您分配给selectedFragment由类名分配的变量。解决办法&nbsp;更换线路EmailAndPasswordFragment&nbsp;=&nbsp;new&nbsp;EmailAndPasswordFieldsFragment();和EmailAndPasswordFragment&nbsp;ePF&nbsp;=&nbsp;new&nbsp;EmailAndPasswordFieldsFragment();和线getSupportFragmentManager().beginTransaction().add(R.id.fields_container,&nbsp;new&nbsp;EmailAndPasswordFieldsFragment()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.commit(); &nbsp;&nbsp;&nbsp;&nbsp;selectedFragment&nbsp;=&nbsp;EmailAndPasswordFragment;和getSupportFragmentManager().beginTransaction().add(R.id.fields_container,&nbsp;ePF) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.commit(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selectedFragment&nbsp;=&nbsp;ePF;并将该行替换&nbsp;if(selectedFragment == EmailAndPasswordFragment)为if(!(selectedFragment instanceof EmailAndPasswordFragment))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java