【发布时间】:2018-01-19 15:47:01
【问题描述】:
从过去几个月开始,我就开始学习 android。我正在创建一个非常基本的聊天应用程序,它使用 Firebase 身份验证、Firebase 实时数据库和 Firebase 存储。我使用了Lapit Chat Application YouTube tutorial 及其source code in GitHub 作为参考。
我的应用程序将 MainActivity 作为启动器活动,Firebase 身份验证在其中检查身份验证是否成功。如果没有,用户将导航到 StartActivity(用户可以登录或注册新帐户)。 MainActivity 有两个 Fragment,分别是 ChatsFragment 和 FriendsFragment,可以滑动导航到彼此(使用 ViewPager)。
问题:应用程序在首次启动时在启动时崩溃。显示'Unfortunately ChaTeX has stopped' 并按 OK 后,应用程序随即启动,一切正常。
我目前正在两台设备上测试 Android Marshmallow 中的这个应用程序,在这两个设备上我都遇到了相同的错误。但是该应用程序根本无法在 Nougat 设备中启动,并且该应用程序一直在崩溃。我猜是两个android版本的错误原因是一样的。
Logcat 在 ChatFragments.java 的 onCreateView 方法中显示 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference 如下面的 sn-p 所示(我得到 Error 的行已被注释):
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 吗?我在这里遗漏了什么吗?
【问题讨论】:
-
我也面临同样的问题。你是怎么解决的?
-
@AlitonOliveira HaroldHibari 的回答解决了我的问题。您需要将该行包装在一个空检查条件中。
标签: android firebase android-fragments firebase-authentication