当我回到MainActivity
所在的ViewPager
时。当我向用户发送消息时,应用程序崩溃。因此,如果数据库中发生某些更改。
我正在使用Windows 10和Android Studio
android {
compileSdkVersion 28
defaultConfig {
applicationId
minSdkVersion 26
targetSdkVersion 28
versionCode 5
versionName "1.5"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
我的代码
public class ChatNotificationFragment extends Fragment {
private FirebaseUser firebaseUser;
private DatabaseReference reference;
public ValueEventListener listener;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_chat_notification, container, false);
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid());
final TabLayout tabLayout = view.findViewById(R.id.tab_layout_chat);
final ViewPager viewPager = view.findViewById(R.id.view_pager_chat);
reference = FirebaseDatabase.getInstance().getReference("Chats");
listener = reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//ChatNotificationFragment.ViewPagerAdapter viewPagerAdapter = new ChatNotificationFragment.ViewPagerAdapter(getFragmentManager());
ChatNotificationFragment.ViewPagerAdapter viewPagerAdapter = new ChatNotificationFragment.ViewPagerAdapter(getChildFragmentManager());
int unread = 0;
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Chat chat = snapshot.getValue(Chat.class);
if (Objects.requireNonNull(chat).getReceiver().equals(firebaseUser.getUid()) && !chat.isIsseen()){
unread++;
}
}
if (unread == 0){
viewPagerAdapter.addFragment(new ChatsFragment(), "Chats");
} else {
viewPagerAdapter.addFragment(new ChatsFragment(), "("+unread+") Chats");
}
viewPagerAdapter.addFragment(new NotificationFragment(), "Notification");
viewPagerAdapter.addFragment(new UsersFragment(), "Users");
viewPagerAdapter.addFragment(new SearchFragment(), "Search");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return view;
}
// class ViewPagerAdapter extends FragmentPagerAdapter {
class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final ArrayList<Fragment> fragments;
private final ArrayList<String> titles;
ViewPagerAdapter(FragmentManager fm){
super(fm);
this.fragments = new ArrayList<>();
this.titles = new ArrayList<>();
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
void addFragment(Fragment fragment, String title){
fragments.add(fragment);
titles.add(title);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}
@Override
public void onDetach() {
super.onDetach();
reference.removeEventListener(listener);
}
}
错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bbinkconnect.bbinktattoo, PID: 22847
java.lang.IllegalStateException: Fragment has not been attached yet.
at androidx.fragment.app.Fragment.instantiateChildFragmentManager(Fragment.java:2383)
at androidx.fragment.app.Fragment.getChildFragmentManager(Fragment.java:845)
at com.bbinkconnect.bbinktattoo.fragments.ChatNotificationFragment$1.onDataChange(ChatNotificationFragment.java:53)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@17.0.0:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@17.0.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@17.0.0:55)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6981)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
答案 0 :(得分:0)
像这样
@Override
public void onDetach() {
super.onDetach();
// remove your listener here
reference.removeEventListener()
}
答案 1 :(得分:0)
您可以考虑如下创建ValueEventListener
,然后将其添加到数据库引用中。
// Declare this publicly in the class level
public ValueEventListener listener;
然后在onResume
函数中定义事件监听器,如下所示,然后将监听器添加到引用中。
@Override
public void onResume {
listener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//ChatNotificationFragment.ViewPagerAdapter viewPagerAdapter = new ChatNotificationFragment.ViewPagerAdapter(getFragmentManager());
ChatNotificationFragment.ViewPagerAdapter viewPagerAdapter = new ChatNotificationFragment.ViewPagerAdapter(getChildFragmentManager());
int unread = 0;
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Chat chat = snapshot.getValue(Chat.class);
if (Objects.requireNonNull(chat).getReceiver().equals(firebaseUser.getUid()) && !chat.isIsseen()){
unread++;
}
}
if (unread == 0){
viewPagerAdapter.addFragment(new ChatsFragment(), "Chats");
} else {
viewPagerAdapter.addFragment(new ChatsFragment(), "("+unread+") Chats");
}
viewPagerAdapter.addFragment(new NotificationFragment(), "Notification");
viewPagerAdapter.addFragment(new SearchFragment(), "Search");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
}
// Add the listener to the database reference now.
reference.addValueEventListener(listener);
}
然后在您的onPause
函数中,您需要删除事件监听器。
@Override
public void onPause() {
super.onDetach();
reference.removeEventListener(myListener);
}
我希望能解决您的问题。