java - 获取 "com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String"

标签 java android

当我想显示通知区域时出现异常,但我不知道问题出在哪里。

通知 fragment

package com.developer.fireone.finga.Fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.developer.fireone.finga.Adapter.NotificationAdapter;
import com.developer.fireone.finga.Model.Chat;
import com.developer.fireone.finga.Model.Notification;
import com.developer.fireone.finga.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;


public class NotificationFragment extends Fragment {

    private RecyclerView recyclerView;
    private NotificationAdapter notificationAdapter;
    private List<Notification> notificationList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_notification, container, false);

        recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(linearLayoutManager);

        notificationList= new ArrayList<>();
        notificationAdapter = new NotificationAdapter(getContext(), notificationList);
        recyclerView.setAdapter(notificationAdapter);

        readNotifications();

        return view;
    }

    private void readNotifications() {

        FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Notifications").child(firebaseUser.getUid());
        reference.keepSynced(true);
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                notificationList.clear();
                for(DataSnapshot snapshot:dataSnapshot.getChildren()){
       Notification notif=snapshot.getValue(Notification.class); //exception
                    notificationList.add(notif);
                }

                Collections.reverse(notificationList);
                notificationAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

}

通知(模型)

package com.developer.fireone.finga.Model;

/**
 * Created by user on 20/12/2018.
 */

public class Notification {

    private String userid;
    private String text;
    private String postid;
    private boolean ispost;

    public Notification() {
    }

    public Notification(String userid, String text, String postid, boolean ispost) {
        this.userid = userid;
        this.text = text;
        this.postid = postid;
        this.ispost = ispost;
    }


    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getPostid() {
        return postid;
    }

    public void setPostid(String postid) {
        this.postid = postid;
    }

    public boolean isIspost() {
        return ispost;
    }

    public void setIspost(boolean ispost) {
        this.ispost = ispost;
    }

}

这是 Firebase 中的通知:

image in firebase

最佳答案

可能在通知模型中,您的变量之一很长,但您将其声明为字符串 例如

    private String userid;
    private String text;
    private String postid;

关于java - 获取 "com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56670161/

相关文章:

java - 如何获取最后一张照片的文件名/uri?

java - 深度优先搜索以查找节点问题之间的所有路径

php - kotlin 通过改造上传图片

android - AdMob 激励视频广告错误(异常)

java - 无法让按钮与其他代码同时工作

java ImageObserver 停止更新动画 gif

java - 在运行时更改 hibernate JPA 属性

java - 如何从 InputStream 中准确读取 4 个字节?

android - 无法在 mac 上的 react-native-navigation 中连接到开发服务器

android - 1.5 和 2.1 之间的 RelativeLayout 差异