我正在使用FirebaseRecycleAdapter来填充评论系统:
评论视图持有人.class:
public class CommentViewHolder extends RecyclerView.ViewHolder {
TextView displayNameView;
TextView bodyView;
String uid;
CommentViewHolder(View itemView) {
super(itemView);
displayNameView = (TextView) itemView.findViewById(R.id.comment_displayName);
bodyView = (TextView) itemView.findViewById(R.id.comment_body);
}
public void setDisplayName(String displayName) {
displayNameView.setText(displayName);
}
public void setBody(String body) {
bodyView.setText(body);
}
public void setUid(String uid) {
this.uid = uid;
}
}
在我的活动中:
mCommentsRecycler = (RecyclerView) findViewById(R.id.recycler_comments);
mCommentsRecycler.setHasFixedSize(false);
mCommentsRecycler.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new FirebaseRecyclerAdapter<Comment, CommentViewHolder>(Comment.class, R.layout.comment_item, CommentViewHolder.class, CommentsReference) {
@Override
protected void populateViewHolder(CommentViewHolder viewHolder, final Comment model, int position) {
viewHolder.setDisplayName(model.getDisplayName());
viewHolder.setBody(model.getBody());
viewHolder.setUid(model.getUid());
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCommentClickedList(model);
}
});
}
};
mCommentsRecycler.setAdapter(mAdapter);
我试图将ViewHolder放入具有"公共静态"的活动内,就像我在某些答案中看到的那样,但同样的错误,
和最有线的部分 - 当我从 ANDROID 工作室运行它时,它工作得很好,一旦我创建调试 apk 或发布 apk,问题就开始出现
错误:
04-10 15:13:16.137 22024-22024/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: PID: 22024
java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View]
at com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:171)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6319)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5507)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5392)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5388)
FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:171)
是Constructor<VH> constructor = mViewHolderClass.getConstructor(View.class);
如果你的ViewHolder
在你的FirebaseRecyclerAdapter
内声明,那么它应该是public
和static
。
在这两种情况下,在 FirebaseRecyclerAdapter
内部声明或在其自己的类上声明,构造函数都必须public
。