我添加此内容的目的是为了更清楚,更容易理解问题。
第一种情况:我启动活动>打开LoginFragment>单击TextView
导航到FragmentActivity
。因此,这里没有错误。现在在第二种情况下,我登录了一个用户>导航到MainActivity
,然后按了注销按钮>导航到LogoutConfirmation
(请记住,这是另一个打开LogoutConfirmation
的活动, (LogoutConfirmation是一个Fragment),然后按Logout按钮注销并>再次导航到LoginFragment
。最后,我按下TextView
再次导航到RegisterFragment
,但这一次它给了我错误。
这是我得到的错误: java.lang.IllegalArgumentException:找不到片段RegisterFragment {5285de6c#2 id = 0x7f07002e}的ID 0x7f07002e(com.example.asus.perfectlogin:id / container)的视图
我在这里注意到为什么系统找不到id:container,因为我登录的时间然后按Logout按钮>导航到LogoutConfirmation
(因为我将此片段称为LogoutFragment extends AppCompatActivity
然后打开它,我使用Intent并调用LogoutFragment
)现在是另一个包含另一个容器的Activity,因为在我的LoginFragment
代码中导航片段,我使用了id:{{1} },但正在运行的活动是container
,其ID为LogoutFragment
。希望您能理解我的所有解释。谢谢!
这是我的代码:container2
扩展了Main Activity
这是我的启动器。
FragmentActity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogout = findViewById(R.id.btnLogout);
mAuth = FirebaseAuth.getInstance();
//mAuth.signOut();
logOut();
setupFirebaseAuth();
}
public void logOut(){
btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, LogoutFragment.class);
startActivity(intent);
}
});
}
/*
-------------------------------Firebase--------------------------------
*/
/*
check if the user is logged in
*/
private void checkCurrentUser(FirebaseUser user){
Log.d(TAG, "checkCurrentUser: checking if user is logged in.");
if (user == null){
Intent intent = new Intent(this, MainFragment.class);
startActivity(intent);
}
}
private void setupFirebaseAuth(){
Log.d(TAG, "setupFirebaseAuth: setting up firebase auth.");
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
//checking if the user is logged in or not
checkCurrentUser(user);
if (user != null){
//User is signed in
Log.d(TAG, "onAuthStateChanged: signed_in: " + user.getUid());
}else {
// User is signed out
Log.d(TAG, "onAuthStateChanged: signed_out");
}
}
};
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
checkCurrentUser(mAuth.getCurrentUser());
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null){
mAuth.removeAuthStateListener(mAuthListener);
}
}
的代码扩展了MainFragment
AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
LoginFragment loginFragment = new LoginFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, loginFragment, loginFragment.getTag()).commit();
}
fragment_main
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container">
</FrameLayout>
的代码扩展了LoginFragment
Fragment
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.login_fragment, container, false);
mEmail = view.findViewById(R.id.tvEmail);
mPassword = view.findViewById(R.id.tvPassword);
mForRegister = view.findViewById(R.id.tvForRegister);
mHeading = view.findViewById(R.id.heading);
eEmail = view.findViewById(R.id.etEmail);
ePassword = view.findViewById(R.id.etPassword);
btnLogin = view.findViewById(R.id.btnLogin);
// Progress Bar and Text view : GONE
mPleaseWait = view.findViewById(R.id.pleaseWait);
mProgressBar = view.findViewById(R.id.progressBar);
mProgressBar.setVisibility(View.GONE);
mPleaseWait.setVisibility(View.GONE);
//Firebase
mAuth = FirebaseAuth.getInstance();
navigatetoRegister();
setupFirebaseAuth();
signInUser();
return view;
}
public void navigatetoRegister(){
mForRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.container, new RegisterFragment());
fr.commit();
}
});
}
private boolean isStringNull(String string){
if (string.equals("")){
return true;
}
return false;
}
/*
---------------------------------Firebase-----------------------------------
*/
private void signInUser(){
// Button = to sign in the user.
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = eEmail.getText().toString();
String password = ePassword.getText().toString();
if (isStringNull(email) && isStringNull(password)){
Toast.makeText(getActivity(), "You must field out all the fields.", Toast.LENGTH_SHORT).show();
}else {
//Progress Bar and TextView be visible
mProgressBar.setVisibility(View.VISIBLE);
mPleaseWait.setVisibility(View.VISIBLE);
//To sign in users
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
Toast.makeText(getActivity(), "Sign in Successful!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getContext(), MainActivity.class);
startActivity(intent);
}else {
mProgressBar.setVisibility(View.GONE);
mPleaseWait.setVisibility(View.GONE);
Toast.makeText(getActivity(), "Could not sign in. Please try again.", Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
}
private void setupFirebaseAuth(){
Log.d(TAG, "setupFirebaseAuth: setting up firebase auth.");
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null){
//User is signed in
Log.d(TAG, "onAuthStateChanged: signed_in: " + user.getUid());
}else {
// User is signed out
Log.d(TAG, "onAuthStateChanged: signed_out");
}
}
};
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null){
mAuth.removeAuthStateListener(mAuthListener);
}
}
的代码扩展了RegisterFragment
Fragment
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.register_fragment, container, false);
mEmail = view.findViewById(R.id.tvEmail);
mPassword = view.findViewById(R.id.tvPassword);
mForLogin = view.findViewById(R.id.tvForLogin);
mHeading = view.findViewById(R.id.heading);
eEmail = view.findViewById(R.id.etEmail);
ePassword = view.findViewById(R.id.etPassword);
btnRegister = view.findViewById(R.id.btnRegister);
// Progress Bar and Text view : GONE
mPleaseWait = view.findViewById(R.id.pleaseWait);
mProgressBar = view.findViewById(R.id.progressBar);
mProgressBar.setVisibility(View.GONE);
mPleaseWait.setVisibility(View.GONE);
navigateToLogin();
registerNewUser();
setupFirebaseAuth();
return view;
}
public void navigateToLogin(){
mForLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.container, new LoginFragment());
fr.commit();
}
});
}
private boolean isStringNull(String string){
if (string.equals("")){
return true;
}
return false;
}
/*
To check if the user is creating a same email.
*/
public void checkUserId(){
if (mAuth.getCurrentUser() != null){
userID = mAuth.getCurrentUser().getUid();
}
}
/*
---------------------------------Firebase-----------------------------------
*/
public void registerNewUser(){
// To register new user
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = eEmail.getText().toString();
String password = ePassword.getText().toString();
if (isStringNull(email) && isStringNull(password)){
Toast.makeText(getContext(), "You must field out all the fields.", Toast.LENGTH_SHORT).show();
}else {
//Progress Bar and TextView be visible
mProgressBar.setVisibility(View.VISIBLE);
mPleaseWait.setVisibility(View.VISIBLE);
//To create/register new user
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
//user is succesfully registered and logged in
checkUserId(); //Checking if the user is create same email
Log.d(TAG, "onComplete: AuthstateChange: " + userID);
Toast.makeText(getActivity(), "Registered Successfully", Toast.LENGTH_SHORT).show();
//AFter Registering account it will navigate to the Login Fragment
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.container, new LoginFragment());
fr.commit();
}else {
mProgressBar.setVisibility(View.GONE);
mPleaseWait.setVisibility(View.GONE);
Toast.makeText(getActivity(), "Please check your internet. Or maybe you registered same email.", Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
}
//This is the listener to check the Firebase if there is user logged in or not.
private void setupFirebaseAuth(){
Log.d(TAG, "setupFirebaseAuth: setting up firebase auth.");
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null){
//User is signed in
Log.d(TAG, "onAuthStateChanged: signed_in: " + user.getUid());
}else {
// User is signed out
Log.d(TAG, "onAuthStateChanged: signed_out");
}
}
};
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null){
mAuth.removeAuthStateListener(mAuthListener);
}
}
的代码扩展了LogoutFragment
AppCompatActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logout_fragment);
LogOutConfirmation fragment = new LogOutConfirmation();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container2, fragment, null).commit();
}
fragment_logout.xml
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container2">
</FrameLayout>
的代码扩展了LogoutConfirmation
Fragment