我正在尝试从Firebase数据库中检索数据,并使用片段在我的时间轴上显示数据。它没有显示错误,但是当我运行应用程序时,它崩溃了
我尝试评论这一点[mInterestList.setAdapter(firebaseRecyclerAdapter);
]它没有崩溃工作
public class TimeLineFragment extends Fragment {
private DatabaseReference mDatabase;
private RecyclerView mInterestList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mDatabase = FirebaseDatabase.getInstance().getReference().child("Interest");
View rootView = inflater.inflate(R.layout.fragment_timeline, container, false);
mInterestList = (RecyclerView) rootView.findViewById(R.id.interest_list);
mInterestList.setHasFixedSize(true);
mInterestList.setLayoutManager(new LinearLayoutManager(getActivity()));
return rootView;
}
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<InterestActivity.Interest,InterestViewHolder > firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<InterestActivity.Interest, InterestViewHolder>(
InterestActivity.Interest.class,
R.layout.interest_row,
InterestViewHolder.class,
mDatabase
) {
@Override
protected void populateViewHolder(InterestViewHolder viewHolder, InterestActivity.Interest model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDescription());
}
};
mInterestList.setAdapter(firebaseRecyclerAdapter);
}
public static class InterestViewHolder extends RecyclerView.ViewHolder {
View mView;
public InterestViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title) {
TextView interest_title = (TextView) mView.findViewById(R.id.Interest_Title);
interest_title.setText(title);
}
public void setDesc(String desc) {
TextView interest_Description = (TextView) mView.findViewById(R.id.interest_Description);
interest_Description.setText(desc);
}
}
}
这是我的InterestActivity Class
public class InterestActivity {
public class Interest {
private String title;
private String description;
private String image;
private String uid;
public Interest(){
}
public Interest(String title, String description, String image, String uid) {
this.title = title;
this.description = description;
this.image = image;
this.uid = uid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
}
}
它没有显示错误,但是当我运行应用程序时它崩溃了。我试着评论这一点[mInterestList.setAdapter(firebaseRecyclerAdapter);
]它没有崩溃。当它崩溃时,我收到以下错误信息。
dRuntime: FATAL EXCEPTION: main
Process: com.example.amaobimills.interestmee, PID: 4350
com.google.firebase.database.DatabaseException: Class com.example.amaobimills.interestmee.InterestActivity$Interest is missing a constructor with no arguments
at com.google.android.gms.internal.zzalq$zza.zze(Unknown Source)
at com.google.android.gms.internal.zzalq$zza.zzcc(Unknown Source)
at com.google.android.gms.internal.zzalq.zzd(Unknown Source)
at com.google.android.gms.internal.zzalq.zzb(Unknown Source)
at com.google.android.gms.internal.zzalq.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:147)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:136)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:176)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6067)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6100)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5282)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5158)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2061)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1445)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1408)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:580)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3379)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3188)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1595)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:323)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:549)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)}
答案 0 :(得分:3)
阅读错误消息
com.google.firebase.database.DatabaseException:类com.example.amaobimills.interestmee.InterestActivity $兴趣缺少一个没有参数的构造函数 ...
为Interest
类提供一个空构造函数
public Interest() {
}
另外,在Interest
中声明static
为InterestActivity
类,如此
public static class Interest {
...
答案 1 :(得分:0)
经过一些尝试,这就是我想出的:
1-在一个单独的文件中创建兴趣类。
2-使用InterestActivity.Interest
Interest
这应该有效