Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用FirebaseAuthentication时,应用程序在首次启动期间崩溃_Android_Firebase_Android Fragments_Firebase Authentication - Fatal编程技术网

Android 使用FirebaseAuthentication时,应用程序在首次启动期间崩溃

Android 使用FirebaseAuthentication时,应用程序在首次启动期间崩溃,android,firebase,android-fragments,firebase-authentication,Android,Firebase,Android Fragments,Firebase Authentication,我从过去几个月开始学习安卓系统。我正在创建一个非常基本的聊天应用程序,它使用Firebase身份验证、Firebase实时数据库和Firebase存储。我使用了和它作为参考 我的应用程序将MainActivity作为启动器活动,其中Firebase身份验证检查身份验证是否成功。否则,用户将被导航到StartActivity(用户可以在这里登录或注册新帐户)。MainActivity有两个片段,即ChatsFragment和FriendsFragment,它们可以滑动以相互导航(使用ViewPag

我从过去几个月开始学习安卓系统。我正在创建一个非常基本的聊天应用程序,它使用Firebase身份验证、Firebase实时数据库和Firebase存储。我使用了和它作为参考

我的应用程序将MainActivity作为启动器活动,其中Firebase身份验证检查身份验证是否成功。否则,用户将被导航到StartActivity(用户可以在这里登录或注册新帐户)。MainActivity有两个片段,即ChatsFragment和FriendsFragment,它们可以滑动以相互导航(使用ViewPager)

问题:应用程序第一次启动时会在启动时崩溃。显示
“不幸的是ChaTeX已停止”
并按OK后,应用程序随即启动,一切正常

我目前正在两台设备上用Android Marshmallow测试这个应用程序,在这两台设备上都出现了相同的错误。但是这款应用程序根本没有在牛轧糖设备上启动,而且应用程序一直在崩溃。我猜两个安卓版本的错误原因是相同的

Logcat显示
java.lang.NullPointerException:尝试在ChatFragments.java的onCreateView方法中的空对象引用上调用虚拟方法“java.lang.String com.google.firebase.auth.FirebaseUser.getUid()”,如下面的代码段所示(注释了我得到错误的那一行):

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);
    }
}
为什么我会犯这个错误?如果身份验证失败,我已经将用户重定向到StartActivity,StartActivity是否应该在第一次启动时启动,而不是创建ViewPager?我在这里遗漏了什么吗?

根据firebase的:

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);
在空检查中,如下所示:

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

我没有为我工作。我的应用程序首先启动MainActivity.java并验证用户是否已登录,例如:
FirebaseAuth.getInstance().addAuthStateListener(新的FirebaseAuth.AuthStateListener(){
,如果(currentUser==null){
,则为负数(
if(currentUser==null){
)我将用户发送到LoginActivity.java。但即使如此,我在ChatFragment.java中也会出现错误,因为
getCurrentUser().getUid()
。即使用if语句包装它,只要在代码中调用FirebaseDatabase,错误就会继续。if(currentUser==null){我面临着同样的问题。你是如何解决的?@AlitonOliveira HaroldHibari的答案解决了我的问题。你需要将这一行包装在空检查条件中。
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);
}