gpt4 book ai didi

java - FragmentManager 从 Activity 到 Fragment 时出错

转载 作者:行者123 更新时间:2023-12-01 16:41:31 25 4
gpt4 key购买 nike

我正在尝试在成功登录后打开一个 fragment 。我意识到 Intent 不能用于 fragment ,因此使用了 FragmentManager。但是,当我尝试使用 FragmentManager 时,我的应用程序崩溃了。我的代码中没有显示任何错误。以下代码显示在我的 Activity 中。

 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 {
//
}
}
}

最佳答案

创建 fragment 的对象,例如

HomeFragment fragment = new HomeFragment();

而不是

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

关于java - FragmentManager 从 Activity 到 Fragment 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61851056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com