我正在尝试使用firebase数据库制作一个recyclerview,这就是我如何创建它
public RecyclerView main;
FirebaseRecyclerAdapter<subject, subjectHolder> mainadapter;
main = (RecyclerView) findViewById(R.id.recyclerview_subject_list);
main.setHasFixedSize(true);
main.setLayoutManager(new LinearLayoutManager(this));
mainadapter = new FirebaseRecyclerAdapter<subject, subjectHolder>(subject.class,R.layout.recyclerview_subject_item,subjectHolder.class, FirebaseDatabase.getInstance().getReference().child("subjets").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("physics")) {
@Override
protected void populateViewHolder(subjectHolder viewHolder, subject model, int position) {
viewHolder.subject.setText(model.getSubject());
viewHolder.teacher.setText(model.getTeacher());
}
};
main.setAdapter(mainadapter);
但错误收到如下
java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View]
at com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:165)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5779)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5003)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3260)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3069)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1505)
at android.support.v7.widget.RecyclerView.access$400(RecyclerView.java:151)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:305)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:814)
at android.view.Choreographer.doCallbacks(Choreographer.java:614)
at android.view.Choreographer.doFrame(Choreographer.java:583)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:800)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5487)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: <init> [class android.view.View]
at java.lang.Class.getConstructorOrMethod(Class.java:472)
at java.lang.Class.getConstructor(Class.java:446)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:162)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5779)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5003)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3260)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3069)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1505)
at android.support.v7.widget.RecyclerView.access$400(RecyclerView.java:151)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:305)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:814)
at android.view.Choreographer.doCallbacks(Choreographer.java:614)
at android.view.Choreographer.doFrame(Choreographer.java:583)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:800)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5487)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
这是我的数据库
**考虑到98794656是用户UID
*家庭作业分支也应该是回收者视图
这里是我的内部主题和subjectHolder
public class subject{
public String subject, teacher;
public subject(String subject, String teacher) {
this.subject = subject;
this.teacher= teacher;
}
public subject() {
this.subject = "subject";
this.teacher= "teacher";
}
}
public class subjectHolder extends RecyclerView.ViewHolder {
public TextView subject , teacher;
public View view;
public subjectHolder (View itemView) {
super(itemView);
view = itemView;
subject = (TextView) itemView.findViewById(R.id.textview_subject_item_subject);
teacher= (TextView) itemView.findViewById(R.id.textview_subject_item_teacher);
}
}
我试图重建,清理项目, 我总是这样做
是因为主题有内在的孩子(家庭作业)引起的问题吗?
同样在subject_item布局中有回收家庭作业,但我还没有做任何事情。
*我之前已经多次正确使用过它,但是在简单的数据中,不是在另一个内部使用recyclerview
任何想法如何解决?!感谢
答案 0 :(得分:3)
ViewHolder类需要公开。如果它们是嵌套的,那么它们也必须是静态的
public class subjectHolder extends RecyclerView.ViewHolder {
需要:
public static class subjectHolder extends RecyclerView.ViewHolder {