1

So all my class is extended as Fragments, then I use the MainActivity to open the HomeFragment.

The problem is when I launch it gives me error and states it is this code where I got error:

private void checkCurrentUser(FirebaseUser user){
    Log.d(TAG, "checkCurrentUser: checking if user is logged in.");

    if (user == null){

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.container, new SignInFragment());
        ft.commit();

    }
}

It is FragmentTransaction ft = getFragmentManager().beginTransaction() that gives me error.

This is the error I got. Process: com.example.asus.loginallfragments, PID: 5081 java.lang.NullPointerException at com.example.asus.loginallfragments.HomeActivity.checkCurrentUser(HomeActivity.java:68) at com.example.asus.loginallfragments.HomeActivity.access$000(HomeActivity.java:19) at com.example.asus.loginallfragments.HomeActivity$2.onAuthStateChanged(HomeActivity.java:85)

This is the code for: MainActivity

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    HomeActivity fragment = new HomeActivity();

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.container, fragment, fragment.getTag());
    ft.commit();

}

This is the code for: HomeActivity (this is extended as Fragment)

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class HomeActivity extends Fragment {

private static final String TAG = "HomeActivity";

TextView mWelcome;
Button btnSignout;
//Firebase
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.home_activity, container, false);

    mWelcome = view.findViewById(R.id.welcome);
    btnSignout = view.findViewById(R.id.btnSignOut);

    setupFirebaseAuth();

    //Firebase
    mAuth = FirebaseAuth.getInstance();

    btnSignout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.container, new SignOutFragment());
            ft.commit();
        }
    });

    return view;
}

/*
-------------------------------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){

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.container, new SignInFragment());
        ft.commit();

    }
}

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);
    }
}
Liza Catherine Ombyaw
  • 804
  • 2
  • 11
  • 27

0 Answers0