【问题标题】:when data is changed in real time database(firebase) app crashes当数据实时更改时,数据库(firebase)应用程序崩溃
【发布时间】:2019-09-12 18:01:57
【问题描述】:

它不仅仅与这些数据有关,错误与 相同 当我将来自其他设备的数据保存到数据库(firebase)中时,应用程序崩溃,其中数据被检索并设置到其他设备的回收视图中

onDataChange 是实时的,当数据更改时会自动调用它
通知类



public class Notifications {
    private String Name;
    private String Email;
    private String Message;
    private String Time;
    private String Date;

    public Notifications() {

    }

    public Notifications(String Name, String Email, String Message, String Time, String Date) {
        this.Name = Name;
        this.Email = Email;
        this.Message = Message;
        this.Time = Time;
        this.Date = Date;
    }

    public String getName() {
        return Name;
    }

    public String getEmail() {
        return Email;
    }

    public String getMessage() {
        return Message;
    }

    public String getTime() {
        return Time;
    }

    public String getDate() {
        return Date;
    }
}
DatabaseReference userref = FirebaseDatabase.getInstance().getReference().child("Notification");
        userref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    notificationList.clear();
                    for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {

                        Notifications notifications = userSnapshot.getValue(Notifications.class);

                        notificationList.add(notifications);

                    }
                    Collections.reverse(notificationList);
                    mAdapter = new Adapter_Notification(notificationList);
                    mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));

                    mRecyclerView.setAdapter(mAdapter);


                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

显示此错误

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Boolean to type com.example.simpleapplocker.Notifications

排队

 notificationList.add(notifications);

启动后检索该数据并显示在回收视图中 这意味着数据存在或者可能创建了通知但没有上传其他数据并且可能调用了这个函数??

【问题讨论】:

  • 先生首先检查您的通知模型类属性数据类型。并检查您的数据更改响应。它是给你对象还是布尔结果。
  • 根据您来自here的请求,我现在要求您将数据库结构添加为JSON文件或至少作为屏幕截图。也请回复@AlexMamo
  • @AlexMamo 感谢您的光临t 检索所有通知
  • @HasnainSabir 我刚刚用图片更新了问题,你可以看看没有布尔值
  • @MuhammadOwaisChaudhary 请同时添加您的Notifications 课程的内容。

标签: android firebase-realtime-database


【解决方案1】:

试试这个方法,对我有用

FirebaseDatabase database = FirebaseDatabase.getInstance();
                DatabaseReference myRef = database.getReference("Notification");
                myRef.addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                        if(dataSnapshot.exist()) {
        Notifications notifications = userSnapshot.getValue(Notifications.class);
        
                                notificationList.add(notifications);
        
                            }
                            Collections.reverse(notificationList);
                            mAdapter = new 
      Adapter_Notification(notificationList);
                            mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
        
                            mRecyclerView.setAdapter(mAdapter);
     
                    }
        
                    @Override
                    public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
        
                    }
        
                    @Override
                    public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
        
                    }
        
                    @Override
                    public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
        
                    }
        
                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {
        
                    }
                });

【讨论】:

    【解决方案2】:
    Notifications notifications = userSnapshot.getValue(Notifications.class);
    

    在这一行中,您会收到一条错误消息,即布尔对象无法转换为通知。如果您将该子项的值设置为 true 或 false(不带双引号),则可能会出现这种情况。再次查看并尝试在要设置为子值的“true”或“false”周围添加双引号。

    【讨论】:

    • 那么它将如何获取所有存在的通知的数据
    • 试试这个方法,让我知道它是否有效。此方法只会在应用启动时调用。
    • 请添加您的数据库结构,也请回复
    • 我刚刚添加了可以查看的图片
    • 使用 Firebase Recycleradapter。 @MuhammadOwaisChaudhary
    猜你喜欢
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 2020-01-19
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    相关资源
    最近更新 更多