我在主要活动中有2个片段选项卡(tab1和tab2)。并且两个选项卡都附有快照侦听器。我还有另一项活动,其中也包括快照侦听器(act1)。并且退出按钮位于tab2上。
Tab1 -----
private RecyclerView recyclerViewChatsView;
private ChatsAdapter usersRecyclerAdapter;
private ArrayList<UsersClass> mUsers;
FirebaseAuth firebaseAuth;
FirebaseUser firebaseUser;
FirebaseFirestore firebaseFirestore;
private ArrayList<String> usersList;
ListenerRegistration usersListenerRegistration;
ListenerRegistration chatsListenerRegistration;
FirebaseAuth.AuthStateListener authStateListener;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_chats, container, false);
recyclerViewChatsView = view.findViewById(R.id.recyclerViewChatsView);
recyclerViewChatsView.setHasFixedSize(true);
recyclerViewChatsView.setLayoutManager(new LinearLayoutManager(getContext()));
return view;
}
@Override
public void onStart() {
super.onStart();
firebaseAuth = FirebaseAuth.getInstance();
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
firebaseFirestore = FirebaseFirestore.getInstance();
usersList = new ArrayList<>();
mUsers = new ArrayList<>();
chatsListenerRegistration = firebaseFirestore.collection("chats")
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots,
@Nullable FirebaseFirestoreException e) {
usersList.clear();
for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
if (doc.getType() == DocumentChange.Type.ADDED) {
ChatsClass chatsClass = doc.getDocument().toObject(ChatsClass.class);
if (chatsClass.getChat_sender().equals(firebaseUser.getUid())) {
usersList.add(chatsClass.getChat_receiver());
}
if (chatsClass.getChat_receiver().equals(firebaseUser.getUid())) {
usersList.add(chatsClass.getChat_sender());
}
}
}
}
});
mUsers.clear();
usersListenerRegistration = firebaseFirestore.collection("users")
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots,
@Nullable FirebaseFirestoreException e) {
for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
if (doc.getType() == DocumentChange.Type.ADDED) {
UsersClass usersClass = doc.getDocument().toObject(UsersClass.class);
if (usersList.contains(usersClass.getUser_uid())) {
mUsers.add(usersClass);
}
}
}
usersRecyclerAdapter = new ChatsAdapter(mUsers, getActivity());
recyclerViewChatsView.setAdapter(usersRecyclerAdapter);
}
});
authStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
} else {
// User is signed out
usersListenerRegistration.remove();
chatsListenerRegistration.remove();
}
}
};
firebaseAuth.addAuthStateListener(authStateListener);
}
选项卡2 -------------------------
private FirebaseAuth firebaseAuth;
private FirebaseUser firebaseUser;
private FirebaseFirestore firebaseFirestore;
private TextView textViewUserName;
private TextView textViewUserGender;
private TextView textViewUserAge;
private TextView textViewUserEmail;
private Button btnAccount;
private Button btnUserSignOut;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
firebaseAuth = FirebaseAuth.getInstance();
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
String currentUser = firebaseUser.getUid();
firebaseFirestore = FirebaseFirestore.getInstance();
textViewUserEmail = (TextView)view.findViewById(R.id.textViewUserEmail);
textViewUserName = (TextView)view.findViewById(R.id.textViewUserName);
textViewUserGender = (TextView)view.findViewById(R.id.textViewUserGender);
textViewUserAge = (TextView)view.findViewById(R.id.textViewUserAge);
btnAccount = (Button)view.findViewById(R.id.btnAccount);
btnUserSignOut = (Button)view.findViewById(R.id.btnUserSignOut);
firebaseFirestore.collection("users")
.document(currentUser)
.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
DocumentSnapshot documentSnapshot = task.getResult();
String user_email = documentSnapshot.getString("user_email");
String user_name = documentSnapshot.getString("user_name");
String user_gender = documentSnapshot.getString("user_gender");
String user_age = documentSnapshot.getString("user_age");
textViewUserEmail.setText(user_email);
textViewUserName.setText(user_name);
textViewUserGender.setText(user_gender);
textViewUserAge.setText(user_age);
}
}
});
btnAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String stringUserEmail = textViewUserEmail.getText().toString();
String stringUserName = textViewUserName.getText().toString();
String stringUserGender = textViewUserGender.getText().toString();
String stringUserAge = textViewUserAge.getText().toString();
Intent intent = new Intent(getActivity(), AccountActivity.class);
intent.putExtra("intentUserEmail", stringUserEmail);
intent.putExtra("intentUserName", stringUserName);
intent.putExtra("intentUserGender", stringUserGender);
intent.putExtra("intentUserAge", stringUserAge);
startActivity(intent);
}
});
btnUserSignOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firebaseAuth.signOut();
Intent intent = new Intent(getActivity(), LoginActivity.class);
startActivity(intent);
}
});
return view;
}
和act1 ------------------
TextView usersProfileUserName;
TextView usersProfileUserGender;
TextView usersProfileUserAge;
String usersProfileUserUid;
Button buttonProfileChat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users_profile);
buttonProfileChat = findViewById(R.id.btnUsersProfileChat);
usersProfileUserName = (TextView) findViewById(R.id.textViewUsersProfileUserName);
usersProfileUserGender = (TextView) findViewById(R.id.textViewUsersProfileUserGender);
usersProfileUserAge = (TextView) findViewById(R.id.textViewUsersProfileUserAge);
usersProfileUserName.setText(getIntent().getStringExtra("user_name"));
usersProfileUserGender.setText(getIntent().getStringExtra("user_gender"));
usersProfileUserAge.setText(getIntent().getStringExtra("user_age"));
usersProfileUserUid = getIntent().getStringExtra("user_uid");
buttonProfileChat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(UsersProfileActivity.this,MessageActivity.class);
intent.putExtra("user_uid", usersProfileUserUid);
startActivity(intent);
}
});
}
现在,当用户跨选项卡片段(选项卡1到选项卡2)移动并注销时,它很好,但是当用户单击选项卡1并转到act1,然后返回到选项卡1并返回选项卡2进行注销时,该应用程序崩溃。
错误是-----
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.google.firebase.firestore.QuerySnapshot.getDocumentChanges()' on a null object reference
at com.sunny.chatapp.ChatsFragment$2.onEvent(ChatsFragment.java:98)
at com.sunny.chatapp.ChatsFragment$2.onEvent(ChatsFragment.java:90)
at com.google.firebase.firestore.zzg.onEvent(Unknown Source:1792)
at com.google.firebase.firestore.g.zzh.zza(SourceFile:28)
at com.google.firebase.firestore.g.zzi.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6635)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
我还在所有三个页面上都附加了FirebaseAuth.AuthStateListener和ListenerRegistration,但仍然崩溃。
我确定某个地方出了问题。看看它。 谢谢。