自从过去几个月以来,我一直在学习android。我正在创建一个使用Firebase身份验证,Firebase实时数据库和Firebase存储的非常基本的聊天应用程序。我使用Lapit Chat Application YouTube tutorial及其source code in GitHub作为参考。
我的应用程序将MainActivity作为启动器活动,其中Firebase身份验证检查身份验证是否成功。如果没有,用户将导航到StartActivity(用户可以登录或注册新帐户)。 MainActivity有两个片段,即ChatsFragment和FriendsFragment,它们可以滑动以相互导航(使用ViewPager)。
问题:第一次启动时,应用在启动时崩溃。显示'Unfortunately ChaTeX has stopped'
并按下确定后,应用程序启动,一切正常。
我目前正在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
,如下面的代码段所示(我收到错误的行被注释):
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?我在这里错过了什么吗?
答案 0 :(得分:1)
根据firebase的documentation:
//--------- GETTING ERROR IN THE FOLLOWING LINE ---------
mCurrent_user_id = mAuth.getCurrentUser().getUid();
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);
}