如何修复com.google.firebase.DatabaseException:未能将java.util.HashMap类型的值转换为字符串

如何修复com.google.firebase.DatabaseException:未能将java.util.HashMap类型的值转换为字符串,java,android,Java,Android,错误为com.google.firebase.database.DatabaseException:未能将java.util.HashMap类型的值转换为字符串 我想在recycler视图中获取数据,但它给了我错误。 我试图解决这个问题,但我做不到。 我给出一些代码片段 Firebase型号: 数据属于我的firebase数据库 "user" : { "9gmWSVMhN4erdOeNCcXw7uB5Itq2" : { "email" : "look

错误为
com.google.firebase.database.DatabaseException:未能将java.util.HashMap类型的值转换为字符串
我想在recycler视图中获取数据,但它给了我错误。 我试图解决这个问题,但我做不到。 我给出一些代码片段

Firebase型号:

数据属于我的firebase数据库

     "user" : {
        "9gmWSVMhN4erdOeNCcXw7uB5Itq2" : {
          "email" : "look@gmail.com",
          "name" : "sagar",
          "password" : "asdfgh",
          "user_id" : "9gmWSVMhN4erdOeNCcXw7uB5Itq2"
        },

        "template" : {

          "FYKw520tCiPxrwi622xzdPzyGCi1" : {
            "-LbWzneRw0zTnE5naR2F" : {
              "c_address" : "aaa",
              "c_logo" : "https://www.freelogodesign.org/Content/img/logo-ex-7.png",
              "c_name" : "aaa",
              "p_designation" : "CTO",
              "p_email" : "yan@com",
              "p_name" : "name",
              "p_phone" : "012"
            },
            "-LbX2_P-KIaCEAkn9I3B" : {
              "c_address" : "bbb",
              "c_logo" : "https://www.freelogodesign.org/Content/img/logo-ex-7.png",
              "c_name" : "bbb",
              "p_designation" : "CEO",
              "p_email" : "gmail",
              "p_name" : "bbbb",
              "p_phone" : "098"
            }
     }
所以我想把孩子们带回家

我尝试使用此代码检索数据


 DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference usersRef = rootRef.child("Accounts").child("Users");
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String email = ds.child("email").getValue(String.class);
                String name = ds.child("name").getValue(String.class);
                Log.d("TAG", email + " / " + name);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    usersRef.addListenerForSingleValueEvent(eventListener);


这是我接收数据的模型类 我认为这门课能很好地获取数据

Template.java


package com.example.look.np.models;

public class Template
{
  private String p_name;
  private String p_designation;
  private String p_email;
  private String p_phone;
  private String c_name;
  private String c_address;
  private String c_logo;
  private String tempID;

  //empty constructor
  public Template()
  {
  }
  //Constructor with variables
  public Template(String p_name, String p_designation, String p_email, String p_phone, String c_name, String c_address, String c_logo, String tempID)
  {
    this.p_name = p_name;
    this.p_designation = p_designation;
    this.p_email = p_email;
    this.p_phone = p_phone;
    this.c_name = c_name;
    this.c_address = c_address;
    this.c_logo = c_logo;
    this.tempID = tempID;
  }
  //Getter and Setter Methods 
  public String getP_name()
  {
    return p_name;
  }

  public void setP_name(String p_name)
  {
    this.p_name = p_name;
  }

  public String getP_designation()
  {
    return p_designation;
  }

  public void setP_designation(String p_designation)
  {
    this.p_designation = p_designation;
  }

  public String getP_email()
  {
    return p_email;
  }

  public void setP_email(String p_email)
  {
    this.p_email = p_email;
  }

  public String getP_phone()
  {
    return p_phone;
  }

  public void setP_phone(String p_phone)
  {
    this.p_phone = p_phone;
  }

  public String getC_name()
  {
    return c_name;
  }

  public void setC_name(String c_name)
  {
    this.c_name = c_name;
  }

  public String getC_address()
  {
    return c_address;
  }

  public void setC_address(String c_address)
  {
    this.c_address = c_address;
  }

  public String getC_logo()
  {
    return c_logo;
  }

  public void setC_logo(String c_logo)
  {
    this.c_logo = c_logo;
  }

  public String getTempID()
  {
    return tempID;
  }

  public void setTempID(String tempID)
  {
    this.tempID = tempID;
  }

}

这是我想在recycler视图中显示数据的片段类

MyCardFragment.class


public class MyCardFragment extends Fragment
{
  private static final String TAG = "MyCardFragment";

  RecyclerView templateRecycler;
  RecyclerviewAdapter adapter;

  List<Template> templateList;

  FirebaseDatabase firebaseDatabase;
  DatabaseReference databaseReference;
  DatabaseReference myref;
  FirebaseUser firebaseUser;
  FirebaseAuth auth;

  String key;

  //This is the empty constructor
  public MyCardFragment()
  {
    // Required empty public constructor
  }

  //This is the oncreate view where i wanna retrieve the data
  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
  {

    View fragmentView = inflater.inflate(R.layout.fragment_mycards, container, false);

    templateRecycler = fragmentView.findViewById(R.id.recyclerView);
    templateRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));

    auth = FirebaseAuth.getInstance();
    firebaseUser = auth.getCurrentUser();
    firebaseDatabase = FirebaseDatabase.getInstance();

    databaseReference = firebaseDatabase.getReference("user").child("template").child(firebaseUser.getUid());

    return fragmentView;
  }


公共类MyCardFragment扩展了片段
{
私有静态最终字符串TAG=“MyCardFragment”;
RecyclerView templateRecycler;
循环水适配器;
列表模板师;
FirebaseDatabase FirebaseDatabase;
数据库参考数据库参考;
数据库参考myref;
FirebaseUser FirebaseUser;
FirebaseAuth-auth;
字符串键;
//这是空的构造函数
公共MyCardFragment()
{
//必需的空公共构造函数
}
//这是我想要检索数据的oncreate视图
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState)
{
视图碎片视图=充气机。充气(R.layout.fragment\u mycards,container,false);
templateRecycler=fragmentView.findViewById(R.id.recyclerView);
setLayoutManager(新的LinearLayoutManager(getActivity());
auth=FirebaseAuth.getInstance();
firebaseUser=auth.getCurrentUser();
firebaseDatabase=firebaseDatabase.getInstance();
databaseReference=firebaseDatabase.getReference(“用户”).child(“模板”).child(firebaseUser.getUid());
返回碎片视图;
}
在下面的onstart方法中,我想获取数据


  @Override
  public void onStart()
  {
    super.onStart();
    //Getting Template List From Database
    //The folloeing code block give me error
    databaseReference.addValueEventListener(new ValueEventListener()
    {
      @Override
      public void onDataChange(@NonNull DataSnapshot dataSnapshot)
      {
        templateList = new ArrayList<>();
        for (DataSnapshot templateSnap : dataSnapshot.getChildren())
        {

          Template template = templateSnap.getValue(Template.class);
          //--->This line gives me error i don 't know what happed here
          //  -- > This line should give what i want
         // -- > I can 't understand whether the model class is incorrect or database refrence
          Log.d(TAG, "onDataChange: " + template.toString());
          templateList.add(template);
        }
        adapter = new RecyclerviewAdapter(getActivity(), templateList);
        templateRecycler.setAdapter(adapter);
      }

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

      }
    });

  }
}


@凌驾
public void onStart()
{
super.onStart();
//从数据库获取模板列表
//下面的代码块给了我一个错误
databaseReference.addValueEventListener(新的ValueEventListener()
{
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot)
{
templateList=新的ArrayList();
对于(DataSnapshot templateSnap:DataSnapshot.getChildren())
{
Template Template=templateSnap.getValue(Template.class);
//--->这行给了我错误我不知道这里发生了什么
//-->这一行应该能满足我的需要
//-->我无法理解模型类是不正确还是数据库引用不正确
Log.d(标记“onDataChange:+template.toString());
templateList.add(模板);
}
适配器=新的RecycleServiceAdapter(getActivity(),templateList);
templateRecycler.setAdapter(适配器);
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError)
{
}
});
}
}
注意:通过使用此代码,数据被检索一次,但我不知道在收到错误后会发生什么


请帮助我,已经两天了,我想修复错误。根据验证数据库引用的代码,添加此语句

Log.i("Uid",firebaseUser.getUid());
databaseReference = firebaseDatabase.getReference("user").child("template").child(firebaseUser.getUid());
添加此行后,检查记录Uid是否返回数据库中的同一密钥

2.如果密钥匹配,那么为了检查模型类,请尝试打印日志

Log.i("template Snap key",templateSnap.child("c_address").getValue(String.class));
Template template = templateSnap.getValue(Template.class);
Log.i("template name",template.c_address);
在这两个日志之后,您将了解问题发生的确切位置,并将这些日志添加到此处,以便我能够了解问题发生的确切位置。 顺便说一句,模型课似乎不错

3.什么是用户的父级


4.其次,在OnDatachange方法之外创建List对象。

这很有趣,但这是IDE的错


我刚刚将IDE更新为最新版本,它解决了我的问题。

您好,信息:我正在使用导航抽屉,其中有一个名为Mycard的导航抽屉项,它引用了MyCardFragment。当我单击Mycard时,它给出了我在Log.d之后的顶部提到的错误(标记“onDataChange:+templateSnap.toString());2019-04-16 22:32:31.139 28389-28389/com.example.look.np D/MyCardFragment:onDataChange:DataSnapshot{key=-LcaVMeriUCrfsg rQOa,value={tempID=-LcaVMeriUCrfsg rQOa,p_电子邮件=yan@com,c_name=aaa,p_phone=012,p_name=name,c_address=aaa,c_logo=,p_designation=CTO}您附加的数据没有tempID字段,但您在日志中获得tempID如何实现这一点,templatesnap中有两个子项,但日志只提供一个子项。更正数据库中的日志或数据。