我想从FireBase检索一些数据以填充RecyclerView。我的应用程序想法是:用户从地图片段中选择一些位置,然后将它们存储在Firebase数据库中。保存的位置显示在Places片段中我想使用RecylcerView。运行应用程序时出现以下错误
08-29 23:01:35.257 25039-25039/com.aplicatiechat.mcristi.firebaseandmapsdemoforlearning E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aplicatiechat.mcristi.firebaseandmapsdemoforlearning, PID: 25039
com.google.firebase.database.DatabaseException: Class com.aplicatiechat.mcristi.firebaseandmapsdemoforlearning.Places is missing a constructor with no arguments
at com.google.android.gms.internal.zzekq.zze(Unknown Source)
at com.google.android.gms.internal.zzekp.zzb(Unknown Source)
at com.google.android.gms.internal.zzekp.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)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1767)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:356)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:559)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
08-29 23:01:35.257 25039-25039/com.aplicatiechat.mcristi.firebaseandmapsdemoforlearning E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aplicatiechat.mcristi.firebaseandmapsdemoforlearning, PID: 25039
com.google.firebase.database.DatabaseException: Class com.aplicatiechat.mcristi.firebaseandmapsdemoforlearning.Places is missing a constructor with no arguments
at com.google.android.gms.internal.zzekq.zze(Unknown Source)
at com.google.android.gms.internal.zzekp.zzb(Unknown Source)
at com.google.android.gms.internal.zzekp.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)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1767)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:356)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:559)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
这是我的地点片段:
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.places_fragment, container, false);
mRecylcerView = mView.findViewById(R.id.places);
mRecylcerView.setHasFixedSize(true);
mRecylcerView.setLayoutManager(new LinearLayoutManager(getContext()));
return mView;
}
@Override
public void onStart() {
super.onStart();
mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
mDataBase = FirebaseDatabase.getInstance().getReference().child("users").child(mFirebaseUser.getUid());
DatabaseReference places = FirebaseDatabase.getInstance().getReference().child("users").child(mFirebaseUser.getUid()).child("place");
DatabaseReference keys = FirebaseDatabase.getInstance().getReference().child("users").child(mFirebaseUser.getUid()).child("key");
FirebaseIndexRecyclerAdapter<Places,PlacesViewHolder> firebaseRecyclerAdapter = new FirebaseIndexRecyclerAdapter<Places,PlacesViewHolder>(Places.class,R.layout.place_layout,PlacesViewHolder.class,places,keys) {
@Override
protected void populateViewHolder(PlacesViewHolder viewHolder, Places model, int position) {
viewHolder.setLat(model.getLat());
viewHolder.setLon(model.getLon());
}
};
mRecylcerView.setAdapter(firebaseRecyclerAdapter);
}
}
class PlacesViewHolder extends RecyclerView.ViewHolder{
View mView;
private TextView lat;
private TextView lon;
public PlacesViewHolder(View itemView) {
super(itemView);
mView = itemView;
lat = mView.findViewById(R.id.lat_view);
lon = mView.findViewById(R.id.lon_view);
}
public void setLat(String n){
lat.setText(n);
}
public void setLon(String e){
lon.setText(e);
}
}
这是我的Model Places类:
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.places_fragment, container, false);
mRecylcerView = mView.findViewById(R.id.places);
mRecylcerView.setHasFixedSize(true);
mRecylcerView.setLayoutManager(new LinearLayoutManager(getContext()));
return mView;
}
@Override
public void onStart() {
super.onStart();
mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
mDataBase = FirebaseDatabase.getInstance().getReference().child("users").child(mFirebaseUser.getUid());
DatabaseReference places = FirebaseDatabase.getInstance().getReference().child("users").child(mFirebaseUser.getUid()).child("place");
DatabaseReference keys = FirebaseDatabase.getInstance().getReference().child("users").child(mFirebaseUser.getUid()).child("key");
FirebaseIndexRecyclerAdapter<Places,PlacesViewHolder> firebaseRecyclerAdapter = new FirebaseIndexRecyclerAdapter<Places,PlacesViewHolder>(Places.class,R.layout.place_layout,PlacesViewHolder.class,places,keys) {
@Override
protected void populateViewHolder(PlacesViewHolder viewHolder, Places model, int position) {
viewHolder.setLat(model.getLat());
viewHolder.setLon(model.getLon());
}
};
mRecylcerView.setAdapter(firebaseRecyclerAdapter);
}
}
class PlacesViewHolder extends RecyclerView.ViewHolder{
View mView;
private TextView lat;
private TextView lon;
public PlacesViewHolder(View itemView) {
super(itemView);
mView = itemView;
lat = mView.findViewById(R.id.lat_view);
lon = mView.findViewById(R.id.lon_view);
}
public void setLat(String n){
lat.setText(n);
}
public void setLon(String e){
lon.setText(e);
}
}
这是我的数据库的屏幕显示: Firebase database
答案 0 :(得分:0)
在您的Places
类中添加一个空的构造函数为
public Places(){}
因为firebase回收适配器在您的Places类中找不到此空构造函数。并替换这个
FirebaseIndexRecyclerAdapter<Places,PlacesViewHolder> firebaseRecyclerAdapter = new FirebaseIndexRecyclerAdapter<Places,PlacesViewHolder>(Places.class,R.layout.place_layout,PlacesViewHolder.class,places,keys)
与
FirebaseIndexRecyclerAdapter<Places,PlacesViewHolder> firebaseRecyclerAdapter = new FirebaseIndexRecyclerAdapter<Places,PlacesViewHolder>(Places.class,R.layout.place_layout,PlacesViewHolder.class,keys,places) {
并且您需要从Places类中删除扩展。