application crashes during the first launch while using FirebaseAuthentication

隐身守侯 提交于 2019-12-11 17:36:00

问题


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?


回答1:


According to firebase's documentation :

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

getCurrentUser can return null, which in this case is happening. Try wrapping the following code

//--------- 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);

in a null check, like so:

if(mAuth.getCurrentUser() != null){
//--------- 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);
}


来源:https://stackoverflow.com/questions/48344748/application-crashes-during-the-first-launch-while-using-firebaseauthentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!