我尝试编写一个片段来列出来自firebase的用户, 但是,它无法显示列表,然后分解应用程序。
任何人都可以帮我解决这个问题吗? 下面是代码:
public class FriendListFragment extends Fragment{
private ArrayList<String> mUsernames = new ArrayList<>();
private ListView mFriendList;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_friendlist,container,false);
//FirebaseDatabase mRef = FirebaseDatabase.getInstance();
DatabaseReference mRef = FirebaseDatabase.getInstance().getReference();
//.getReferenceFromUrl("https://game-frontier.firebaseio.com/");
mFriendList = (ListView) rootView.findViewById(R.id.friendList);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mUsernames);
//final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(android.R.layout.simple_list_item_1, mUsernames);
//mFriendList.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mUsernames));
mFriendList.setAdapter(arrayAdapter);
mRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.getValue(String.class);
mUsernames.add(value);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return rootView;
}
}
错误logcat如下:
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 1
W/PathParser: Points are too far apart 4.000000596046461
D/AndroidRuntime: Shutting down VM
E/UncaughtException: com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String
at com.google.android.gms.internal.zzbtg.zzaF(Unknown Source)
at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at game_frontier_fragment.FriendListFragment$1.onChildAdded(FriendListFragment.java:58)
at com.google.android.gms.internal.zzbox.zza(Unknown Source)
at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source)
at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
D / FA:记录事件(FE):_ae,Bundle [{_ o = crash,_sc = MainActivity,_si = -9082221781176828208,timestamp = 1487772508300,fatal = 1}] E / AndroidRuntime:致命异常:主要 处理:com.gamefrontier.jk.gamefrontier,PID:14745 com.google.firebase.database.DatabaseException:无法将java.util.HashMap类型的值转换为String 在com.google.android.gms.internal.zzbtg.zzaF(未知来源) 在com.google.android.gms.internal.zzbtg.zzb(未知来源) 在com.google.android.gms.internal.zzbtg.zza(未知来源) 在com.google.firebase.database.DataSnapshot.getValue(未知来源) at game_frontier_fragment.FriendListFragment $ 1.onChildAdded(FriendListFragment.java:58) 在com.google.android.gms.internal.zzbox.zza(未知来源) 在com.google.android.gms.internal.zzbqx.zzZS(未知来源) 在com.google.android.gms.internal.zzbra $ 1.run(未知来源) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:234) 在android.app.ActivityThread.main(ActivityThread.java:5526) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) I / DpmTcmClient:RegisterTcmMonitor来自:com.android.okhttp.TcmIdleTimerMonitor I /处理:发送信号。 PID:14745 SIG:9 申请已终止。