从活动到片段的FragmentManager中的错误

我试图在成功登录后打开一个片段。我意识到意图不能用于片段,因此可以使用FragmentManager。但是,当我尝试使用FragmentManager时,我的应用程序崩溃了。我没有错误出现在代码中。我的活动中显示了以下代码。

 package com.example.ireview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.ireview.ui.home.HomeFragment;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class LogInPage extends AppCompatActivity {

    private FirebaseAuth mAuth = FirebaseAuth.getInstance();
    private FirebaseUser currentUser = mAuth.getCurrentUser();
    EditText email;
    EditText password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in_page);

        email = (EditText) findViewById(R.id.editTextEmailLogIn);
        password = (EditText) findViewById(R.id.editTextPasswordLogIn);

    }


    public void clickLogin(View v) {
        String userEmail = email.getText().toString();
        String userPassword = password.getText().toString();

        if (userEmail.length() != 0 && userPassword.length() != 0) {
            mAuth.signInWithEmailAndPassword(userEmail, userPassword)
                    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
                                // Sign up success
                                Toast.makeText(getBaseContext(), "Login Successful.",
                                        Toast.LENGTH_SHORT).show();
                                // Bring user to success activity

                                androidx.fragment.app.Fragment fragment = new Fragment();
                                FragmentManager fragmentManager = getSupportFragmentManager();
                                fragmentManager.beginTransaction().replace(R.id.home_frag, fragment).commit();



                            } else {
                                //
                            }
                        }
                    });
        } else {
            //
        }
    }
}
评论
  • 偏执旳温柔
    偏执旳温柔 回复

    create object of your fragment like

    HomeFragment fragment = new HomeFragment();
    

    代替

    androidx.fragment.app.Fragment fragment = new Fragment();