日志 这就是我得到的
我遇到以下错误:com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为类型
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.pro.mychatapp.Friends
at com.google.android.gms.internal.zg.zzb(Unknown Source)
at com.google.android.gms.internal.zg.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:22)
at com.firebase.ui.database.ObservableSnapshotArray.getObject(ObservableSnapshotArray.java:141)
at com.firebase.ui.database.CachingObservableSnapshotArray.getObject(CachingObservableSnapshotArray.java:40)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:127)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:164)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
FirendsFragment.java
我只想显示朋友列表
package com.example.pro.mychatapp;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
/**
* A simple {@link Fragment} subclass.
*/
public class FriendsFragment extends Fragment {
private RecyclerView myFriendsList;
private DatabaseReference FriendsReference;
private FirebaseAuth mAuth;
String online_user_id;
private View myMainView;
public FriendsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myMainView = inflater.inflate(R.layout.fragment_friends, container, false);
myFriendsList = (RecyclerView) myMainView.findViewById(R.id.friends_list);
mAuth = FirebaseAuth.getInstance() ;
online_user_id = mAuth.getCurrentUser().getUid();
FriendsReference = FirebaseDatabase.getInstance().getReference().child("Friends").child(online_user_id);
myFriendsList.setLayoutManager(new LinearLayoutManager(getContext()));
// Inflate the layout for this fragment
return myMainView;
}
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Friends, FriendsViewHolder > firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Friends, FriendsViewHolder>
(
Friends.class,
R.layout.all_users_display_layout,
FriendsViewHolder.class,
FriendsReference
)
{
@Override
protected void populateViewHolder(FriendsViewHolder viewHolder, Friends model, int position)
{
System.out.println("********************************************************************");
viewHolder.setDate(model.getDate());
}
};
myFriendsList.setAdapter(firebaseRecyclerAdapter);
}
public static class FriendsViewHolder extends RecyclerView.ViewHolder
{
View mView;
public FriendsViewHolder(View itemView)
{
super(itemView);
mView =itemView;
}
public void setDate(String date)
{
TextView sinceFriendsDate = (TextView) mView.findViewById(R.id.all_users_status);
sinceFriendsDate.setText(date);
}
}
}
Friends.java
package com.example.pro.mychatapp;
public class Friends {
public String date;
public Friends()
{
}
public Friends(String date)
{
this.date = date;
}
public String getDate()
{
return date;
}
public void setDate(String date)
{
this.date = date;
}
}
我可以知道这是什么问题吗?我是firebase的新手,我需要你的帮助。我在这里读了一些类似的问题,但我仍然无法解决我的问题。这里有专家可以帮助我吗?
答案 0 :(得分:0)
从我能看到的情况来看,这里可能会发生一些事情。
1。)&#34; date&#34;的数据。在解析数据快照时,Friends模型上的属性无法与命名约定匹配 - &gt;宾语。我无法在数据库中看到您的对象示例,因此只需要澄清一下。
2。)这行代码似乎是检索一个子对象,它是一个键而不是Friend对象。
FriendsReference = FirebaseDatabase.getInstance().getReference().child("Friends").child(online_user_id);
相反,我假设您要使用FirebaseListOptions并填充指定用户朋友的查询。
3。)以下是您尝试执行的基本案例的example, 我肯定会用它作为参考。
基于错误