Android 从Firebase数据库检索数据并显示在RecyclerView上
当我运行应用程序时,它崩溃了,但是在我的代码中我没有看到任何错误。我正在尝试将firebase上的数据检索到Android 从Firebase数据库检索数据并显示在RecyclerView上,android,firebase,firebase-realtime-database,android-recyclerview,firebaseui,Android,Firebase,Firebase Realtime Database,Android Recyclerview,Firebaseui,当我运行应用程序时,它崩溃了,但是在我的代码中我没有看到任何错误。我正在尝试将firebase上的数据检索到recyclerview。我正在制作一个帖子评论应用程序。请帮我看看我的代码有什么问题 Java代码 public class DialogComments extends AppCompatActivity implements View.OnClickListener { private FirebaseAuth mAuithComment; private Prog
recyclerview
。我正在制作一个帖子评论应用程序。请帮我看看我的代码有什么问题
Java代码
public class DialogComments extends AppCompatActivity implements View.OnClickListener {
private FirebaseAuth mAuithComment;
private ProgressDialog mProgressDialog;
private EditText etComment;
private DatabaseReference dbRef;
private FirebaseDatabase mDatabaseComment;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog_comments);
recyclerView = (RecyclerView) findViewById(R.id.comment_recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mDatabaseComment = FirebaseDatabase.getInstance();
dbRef = mDatabaseComment.getReference("UserComments");
}
@Override
protected void onStart(){
super.onStart();
FirebaseRecyclerAdapter<CommentModels, CommentViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<CommentModels, CommentViewHolder>
(
CommentModels.class,
R.layout.row_comment,
CommentViewHolder.class,
dbRef
)
{
@Override
protected void populateViewHolder(CommentViewHolder viewHolder, CommentModels model, int position) {
viewHolder.setusername(model.getusername());
viewHolder.setcomment(model.getcomment());
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class CommentViewHolder extends RecyclerView.ViewHolder{
TextView usernameTextView;
TextView commentTextView;
View mView;
public CommentViewHolder(View itemView){
super(itemView);
mView = itemView;
usernameTextView = (TextView) mView.findViewById(R.id.tv_username);
commentTextView = (TextView) mView.findViewById(R.id.tv_comment);
}
public void setusername(String username){
usernameTextView.setText(username);
}
public void setcomment(String comment){
commentTextView.setText(comment);
}
}
public void init(){
etComment = (EditText) findViewById(R.id.et_comment);
findViewById(R.id.iv_send).setOnClickListener(this);
}
public void sendcomment(){
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.iv_send:
sendcomment();
}
}
}
活动对话框注释.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/tv_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Username"
android:textColor="@android:color/black"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="dfsd; lfjsd;fk js;dkfj a;sdfj ;sadfj ;sadlkfj ;asdlkfj a;ldskfj ;alkdjfs ;alksdjf ;alkjdsf"
android:textColor="@android:color/black" />
</LinearLayout>
<!-- <TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:text="10h30" /
</LinearLayout>
</android.support.v7.widget.CardView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="450dp"
tools:context="com.feu.teamanonymous.ayokotseph_final.Activities.DialogComments"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/comment_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<EditText
android:id="@+id/et_comment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type comment..." />
<ImageView
android:id="@+id/iv_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:src="@drawable/ic_send_black_24dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
您是否收到任何错误消息或异常?或者你知道错误发生在哪一行吗?是的,我有一个致命的异常。致命异常:主进程:com.feu.teamanonymous.ayokotseph_final,PID:32744 com.google.firebase.database.DatabaseException:Class com.feu.teamanonymous.ayokotseph_final.Variables.CommentModels缺少一个没有参数的构造函数问题已解决:刚刚在名为public的CommentModels类上添加了一个空白方法CommentModels(){}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="450dp"
tools:context="com.feu.teamanonymous.ayokotseph_final.Activities.DialogComments"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/comment_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<EditText
android:id="@+id/et_comment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type comment..." />
<ImageView
android:id="@+id/iv_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:src="@drawable/ic_send_black_24dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>