本文介绍了使用FirebaseAuthentication时,应用程序在首次启动期间崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从过去的几个月开始,我就开始学习android.我正在创建一个使用Firebase身份验证,Firebase实时数据库和Firebase存储的非常基本的聊天应用程序.我使用了 Lapit Chat Application YouTube教程及其 GitHub中的源代码作为参考.

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.

我的应用程序具有MainActivity作为启动程序活动,Firebase身份验证在此检查身份验证是否成功.如果不是,则将用户导航到StartActivity(用户可以在其中登录或注册新帐户).MainActivity有两个片段,分别是ChatsFragment和FriendsFragment,可以滑动以彼此导航(使用ViewPager).

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).

问题:该应用在首次启动时会在启动时崩溃.显示不幸的是ChaTeX已停止" ,然后按OK,然后启动应用程序,一切正常.

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.

我目前正在两个设备上的Android Marshmallow中测试此应用程序,但两个设备都遇到相同的错误.但是该应用程序根本无法在Nougat设备中启动,并且该应用程序一直崩溃.我猜是两个Android版本中的错误原因都是相同的.

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显示 java.lang.NullPointerException:尝试在null对象引用上调用虚拟方法'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()'ChatFragments.java的onCreateView方法,如下面的代码片段所示(我收到Error的行被注释):

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:

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:

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

为什么会出现此错误?由于如果身份验证失败,我已将用户重定向到StartActivity,因此StartActivity不应在首次启动期间启动而不是创建ViewPager吗?我在这里想念什么吗?

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?

推荐答案

根据firebase的文档:

According to firebase's documentation :

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

getCurrentUser可以返回null,这种情况正在发生.尝试包装以下代码

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

这篇关于使用FirebaseAuthentication时,应用程序在首次启动期间崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:30