Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've been starting to learn android since past few months. I'm creating a very basic chat application that uses Firebase Authentication, Firebase realtime database and Firebase Storage. I have used Lapit Chat Application YouTube tutorial and its source code in GitHub as the reference.

My application has MainActivity as the launcher activity, where the Firebase Authentication checks whether authentication is successful or not. If not, user is navigated to StartActivity (where user can login or register a new account). MainActivity has two fragments, namely ChatsFragment and FriendsFragment, which can be slided to navigate to each other (ViewPager is used).

Problem: The app crashes at startup when launched for the first time. After showing 'Unfortunately ChaTeX has stopped' and pressing okay, the app then launches, and everything works fine.

I am currently testing this application in Android Marshmallow in two devices where I get the same error in both devices. But the application doesn't launch at all in Nougat devices and the app keeps crashing all the time. What I guess is that the cause of error in both the android versions is same.

Logcat shows java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference in the onCreateView method of ChatFragments.java as shown in the snippet below(The line where I get Error is commented):

ChatsFragment.java:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    mMainView = inflater.inflate(R.layout.fragment_chats, container, false);

    mConvList = (RecyclerView) mMainView.findViewById(R.id.conv_list);
    mAuth = FirebaseAuth.getInstance();


        //--------- GETTING ERROR IN THE FOLLOWING LINE ---------
        mCurrent_user_id = mAuth.getCurrentUser().getUid();

        mConvDatabase = FirebaseDatabase.getInstance().getReference().child("Chat").child(mCurrent_user_id);

        mConvDatabase.keepSynced(true);
        mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
        mMessageDatabase = FirebaseDatabase.getInstance().getReference().child("messages").child(mCurrent_user_id);
        mUsersDatabase.keepSynced(true);

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    linearLayoutManager.setReverseLayout(true);
    linearLayoutManager.setStackFromEnd(true);

    mConvList.setHasFixedSize(true);
    mConvList.setLayoutManager(linearLayoutManager);

    return mMainView;
}

MainActivity.java :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mAuth = FirebaseAuth.getInstance();

    if (mAuth.getCurrentUser() != null) {
        mUserRef = FirebaseDatabase.getInstance().getReference().child("Users").child(mAuth.getCurrentUser().getUid());
    }

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                mUserRef.child("online").setValue("true");
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
            } else {
                // User is not signed in
                Intent startIntent = new Intent(MainActivity.this, StartActivity.class);
                startActivity(startIntent);
                finish();
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
        }
    };

    //Tabs in the main page
    mViewPager = (ViewPager) findViewById(R.id.main_tabPager);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mTablayout = (TabLayout) findViewById(R.id.main_tabs);
    mTablayout.setupWithViewPager(mViewPager);
}

@Override
public void onStart(){
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}

public void onStop() {
    super.onStop();

    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }
}

Why am I getting this error? As I have redirected the user to StartActivity if the authentication fails, shouldn't StartActivity start during first launch instead of creating ViewPager? Am I missing something here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
174 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...