4 回答
TA贡献1860条经验 获得超8个赞
从我所看到的是,您正在初始化片段并将其存储在变量 EmailAndPasswordFragment 中,但是在将片段添加到容器时,您再次重新初始化它。
这就是为什么它给出空指针异常。
这是你的onCreate
getSupportFragmentManager().beginTransaction().add(R.id.fields_container, EmailAndPasswordFragment) .commit();
TA贡献1817条经验 获得超6个赞
更新:哦天哪,我很抱歉大家,我只是忘记初始化 UserSingleton 类。
刚刚添加到 onCreate 方法的下一行: userSingleton = UserSingleton.getInstance();
xml 中有一个新代码
public class LoginActivity extends AppCompatActivity implements LoginModel {
private LoginPresenter presenter;
private Fragment emailAndPasswordFragment;
private Fragment 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, emailAndPasswordFragment)
.commit();
selectedFragment = emailAndPasswordFragment;
}
public void onClickSignInAndUpButtons(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, emailAndPasswordFragment)
.commit();
selectedFragment = emailAndPasswordFragment;
}
break;
case R.id.btnGetStarted:
//TODO edd signup methos
break;
}
}
登录活动 XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradien_color"
tools:context=".Screens.Login.View.LoginActivity">
<ImageView
android:id="@+id/logoMain"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:layout_marginTop="52dp"
android:layout_marginEnd="8dp"
android:contentDescription="@string/logo_main"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo_main" />
<FrameLayout
android:id="@+id/fields_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/cvGetStarted"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/logoMain">
</FrameLayout>
<androidx.cardview.widget.CardView
android:id="@+id/cvGetStarted"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="20dp"
android:translationZ="8dp"
app:layout_constraintBottom_toTopOf="@+id/btnSignIn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.456"
app:layout_constraintStart_toStartOf="parent"
card_view:cardCornerRadius="25dp"
card_view:cardElevation="3dp">
<Button
android:id="@+id/btnGetStarted"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/register_button_style"
android:text="@string/btn_get_started"
android:textColor="@color/white" />
</androidx.cardview.widget.CardView>
<Button
android:id="@+id/btnSignIn"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginBottom="48dp"
android:background="@null"
android:onClick="onClickSignInAndUpButtons"
android:stateListAnimator="@null"
android:text="@string/btn_sign_in"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
片段类
public class EmailAndPasswordFieldsFragment extends Fragment {
private EditText etEmail, etPassword;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_email_and_password_fields, container, false);
etEmail = view.findViewById(R.id.etEmail);
etPassword = view.findViewById(R.id.etPassword);
return view;
}
}
片段 xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Screens.Login.View.UserDetailsFragment">
<LinearLayout
android:layout_width="350dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:orientation="vertical"
android:gravity="center">
<androidx.cardview.widget.CardView
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="12dp"
android:translationZ="8dp"
card_view:cardBackgroundColor="@android:color/transparent"
card_view:cardCornerRadius="25dp"
card_view:cardElevation="3dp">
<EditText
android:id="@+id/etEmail"
android:layout_width="250dp"
android:layout_height="40dp"
android:background="@drawable/input_field_style"
android:drawableStart="@drawable/login_icon"
android:drawableTint="@color/tint_and_text_color"
android:ems="10"
android:hint="@string/hint_email"
android:inputType="textEmailAddress"
android:textColor="@color/main_text_color"
android:textColorHint="@color/tint_and_text_color"
android:textSize="16sp" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="12dp"
android:translationZ="8dp"
card_view:cardBackgroundColor="@android:color/transparent"
card_view:cardCornerRadius="25dp"
card_view:cardElevation="3dp">
<EditText
android:id="@+id/etPassword"
android:layout_width="250dp"
android:layout_height="40dp"
android:background="@drawable/input_field_style"
android:drawableStart="@drawable/password_icon"
android:drawableTint="@color/tint_and_text_color"
android:ems="10"
android:hint="@string/hint_password"
android:inputType="textPassword"
android:textColor="@color/main_text_color"
android:textColorHint="@color/tint_and_text_color"
android:textSize="16sp" />
</androidx.cardview.widget.CardView>
</LinearLayout>
</FrameLayout>
TA贡献1852条经验 获得超7个赞
EmailAndPasswordFragment = new EmailAndPasswordFieldsFragment();
这是 EmailAndPasswordFragment 的第一个实例,在 add 方法中您将传递 EmailAndPasswordFragment 的新实例
在 onclick 内部,您正在访问第一个实例的值。
TA贡献1828条经验 获得超6个赞
你的线路有问题
EmailAndPasswordFragment = new EmailAndPasswordFieldsFragment();
因为您正在创建 Fragment 对象,但将其分配给类名,而不是变量,然后您分配给selectedFragment
由类名分配的变量。
解决办法 更换线路
EmailAndPasswordFragment = new EmailAndPasswordFieldsFragment();
和
EmailAndPasswordFragment ePF = new EmailAndPasswordFieldsFragment();
和线
getSupportFragmentManager().beginTransaction().add(R.id.fields_container, new EmailAndPasswordFieldsFragment()) .commit(); selectedFragment = EmailAndPasswordFragment;
和
getSupportFragmentManager().beginTransaction().add(R.id.fields_container, ePF) .commit(); selectedFragment = ePF;
并将该行替换 if(selectedFragment == EmailAndPasswordFragment)
为if(!(selectedFragment instanceof EmailAndPasswordFragment))
添加回答
举报