recyclerview未显示firebase firestore和Crash应用程序中的数据

lrl1mhuk  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(373)

试图让我的recyclerview显示firestore中的数据,但它使我的应用程序崩溃,我不知道为什么。我看了很多教程,但都没用,我一步一步地跟着教程,不太清楚为什么教程可以用,而我的却不行。任何帮助都将不胜感激。
建立梯度

  1. plugins {
  2. id 'com.android.application'
  3. id 'com.google.gms.google-services'
  4. }
  5. android {
  6. compileSdkVersion 30
  7. buildToolsVersion "30.0.3"
  8. defaultConfig {
  9. applicationId "com.example.orbital"
  10. minSdkVersion 21
  11. targetSdkVersion 30
  12. versionCode 1
  13. versionName "1.0"
  14. testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
  15. }
  16. buildTypes {
  17. release {
  18. minifyEnabled false
  19. proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  20. }
  21. }
  22. compileOptions {
  23. sourceCompatibility JavaVersion.VERSION_1_8
  24. targetCompatibility JavaVersion.VERSION_1_8
  25. }
  26. }
  27. dependencies {
  28. implementation 'com.google.android.material:material:1.3.0'
  29. implementation 'androidx.appcompat:appcompat:1.3.0'
  30. implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
  31. implementation 'androidx.cardview:cardview:1.0.0'
  32. implementation 'com.google.firebase:firebase-auth:21.0.1'
  33. implementation 'com.google.firebase:firebase-firestore:23.0.1'
  34. implementation 'com.google.firebase:firebase-database:20.0.0'
  35. implementation 'com.google.firebase:firebase-storage:20.0.0'
  36. implementation 'com.airbnb.android:lottie:3.4.1'
  37. implementation 'com.firebaseui:firebase-ui-firestore:7.2.0'
  38. implementation 'androidx.recyclerview:recyclerview:1.0.0'
  39. testImplementation 'junit:junit:4.+'
  40. implementation 'androidx.legacy:legacy-support-v4:1.0.0'
  41. androidTestImplementation 'androidx.test.ext:junit:1.1.2'
  42. androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
  43. }

从firestore检索数据的聊天片段

  1. package com.example.orbital;
  2. import android.graphics.Color;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9. import androidx.annotation.NonNull;
  10. import androidx.annotation.Nullable;
  11. import androidx.fragment.app.Fragment;
  12. import androidx.recyclerview.widget.LinearLayoutManager;
  13. import androidx.recyclerview.widget.RecyclerView;
  14. import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
  15. import com.firebase.ui.firestore.FirestoreRecyclerOptions;
  16. import com.google.firebase.auth.FirebaseAuth;
  17. import com.google.firebase.firestore.FirebaseFirestore;
  18. import com.google.firebase.firestore.FirebaseFirestoreException;
  19. import com.google.firebase.firestore.Query;
  20. public class chatFragment extends Fragment {
  21. private FirebaseFirestore firebaseFirestore;
  22. LinearLayoutManager linearLayoutManager;
  23. private FirebaseAuth firebaseAuth;
  24. FirestoreRecyclerAdapter<UserModel, NoteViewHolder> chatAdapter;
  25. RecyclerView mRecyclerView;
  26. @Nullable
  27. @Override
  28. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  29. View v = inflater.inflate(R.layout.chatfragment,container,false);
  30. firebaseAuth = FirebaseAuth.getInstance();
  31. firebaseFirestore = FirebaseFirestore.getInstance();
  32. mRecyclerView = v.findViewById(R.id.recyclerView);
  33. Query query = firebaseFirestore.collection("Users");
  34. FirestoreRecyclerOptions<UserModel> allusername = new FirestoreRecyclerOptions.Builder<UserModel>()
  35. .setQuery(query,UserModel.class)
  36. .build();
  37. chatAdapter = new FirestoreRecyclerAdapter<UserModel, NoteViewHolder>(allusername) {
  38. @Override
  39. public void onError(FirebaseFirestoreException e) {
  40. // Called when there is an error getting a query snapshot. You may want to update
  41. // your UI to display an error message to the user.
  42. // ...
  43. }
  44. @Override
  45. protected void onBindViewHolder(@NonNull NoteViewHolder noteViewHolder, int i, @NonNull UserModel userModel) {
  46. noteViewHolder.particularusername.setText(userModel.getName());
  47. //fetch image
  48. /*if(UserModel.getStatus().equals("Online"))
  49. {
  50. holder.statusofuser.setText(model.getStatus());
  51. holder.statusofuser.setTextColor(Color.GREEN);
  52. }
  53. else
  54. {
  55. holder.statusofuser.setText(model.getStatus());
  56. }*/
  57. noteViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
  58. @Override
  59. public void onClick(View v) {
  60. Toast.makeText(getActivity(),"Chat Clicked",Toast.LENGTH_SHORT).show();
  61. }
  62. });
  63. }
  64. @NonNull
  65. @Override
  66. public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  67. View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.chatview_layout,parent,false);
  68. return new NoteViewHolder(view);
  69. }
  70. };
  71. //mRecyclerView.setHasFixedSize(true);
  72. linearLayoutManager=new LinearLayoutManager(getContext());
  73. linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
  74. mRecyclerView.setLayoutManager(linearLayoutManager);
  75. mRecyclerView.setAdapter(chatAdapter);
  76. return v;
  77. }
  78. public class NoteViewHolder extends RecyclerView.ViewHolder
  79. {
  80. //suppose to add on image
  81. private TextView particularusername;
  82. //private TextView statusofuser;
  83. public NoteViewHolder(@NonNull View itemView) {
  84. super(itemView);
  85. particularusername=itemView.findViewById(R.id.nameofuser);
  86. //statusofuser=itemView.findViewById(R.id.statusofuser);
  87. }
  88. }
  89. @Override
  90. public void onStart() {
  91. super.onStart();
  92. chatAdapter.startListening();
  93. }
  94. @Override
  95. public void onStop() {
  96. super.onStop();
  97. chatAdapter.stopListening();
  98. }
  99. }

chatviewlayout(xml)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@color/white">
  6. <androidx.recyclerview.widget.RecyclerView
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:id="@+id/recyclerview">
  10. </androidx.recyclerview.widget.RecyclerView>
  11. </RelativeLayout>

聊天片段(xml)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="90dp"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. android:background="@color/smokyWhite">
  7. <RelativeLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:paddingLeft="10dp"
  11. android:paddingRight="10dp"
  12. android:paddingTop="10dp"
  13. android:paddingBottom="-5dp">
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="Name Display Here"
  18. android:layout_toRightOf="@id/cardviewofuser"
  19. android:layout_marginTop="15dp"
  20. android:layout_marginLeft="15dp"
  21. android:textSize="20sp"
  22. android:id="@+id/nameofuser"
  23. android:textStyle="bold"
  24. android:textColor="#0b0b0b">
  25. </TextView>
  26. <TextView
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:text="Display Status here"
  30. android:textSize="12sp"
  31. android:layout_marginLeft="15dp"
  32. android:layout_marginTop="3dp"
  33. android:layout_toRightOf="@id/cardviewofuser"
  34. android:id="@+id/statusofuser"
  35. android:layout_below="@id/nameofuser"
  36. android:textColor="#6a6a6a">
  37. </TextView>
  38. <androidx.cardview.widget.CardView
  39. android:layout_width="55dp"
  40. android:layout_height="55dp"
  41. android:layout_marginBottom="20dp"
  42. android:layout_alignParentLeft="true"
  43. android:layout_centerVertical="true"
  44. android:id="@+id/cardviewofuser"
  45. app:cardCornerRadius="55dp"
  46. >
  47. <ImageView
  48. android:layout_width="match_parent"
  49. android:layout_height="match_parent"
  50. android:src="@drawable/ic_launcher_background"
  51. android:id="@+id/imageviewofuser"
  52. android:scaleType="centerCrop">
  53. </ImageView>
  54. </androidx.cardview.widget.CardView>
  55. <RelativeLayout
  56. android:layout_width="match_parent"
  57. android:layout_height="0.1dp"
  58. android:layout_marginLeft="10dp"
  59. android:backgroundTint="#a6a6a6"
  60. android:background="#a6a6a6"
  61. android:layout_below="@id/cardviewofuser"
  62. android:layout_toRightOf="@id/cardviewofuser"
  63. android:layout_marginTop="-5dp" />
  64. </RelativeLayout>
  65. </RelativeLayout>

用户模型

  1. package com.example.orbital;
  2. public class UserModel {
  3. String Name, Contact, Address, Gender;
  4. public UserModel(String Name, String Contact, String Address, String Gender) {
  5. this.Name = Name;
  6. this.Contact = Contact;
  7. this.Address = Address;
  8. this.Gender = Gender;
  9. }
  10. public UserModel() {
  11. }
  12. public String getName() {
  13. return Name;
  14. }
  15. public void setName(String Name) {
  16. this.Name = Name;
  17. }
  18. public String getContact() {
  19. return Contact;
  20. }
  21. public void setContact(String Contact) {
  22. this.Contact = Contact;
  23. }
  24. public String getAddress() {
  25. return Address;
  26. }
  27. public void setAddress(String Address) {
  28. this.Address = Address;
  29. }
  30. public String getGender() {
  31. return Gender;
  32. }
  33. public void setGender(String Gender) {
  34. this.Gender = Gender;
  35. }
  36. }
0yycz8jy

0yycz8jy1#

代码中的问题在于,您使用的recyclerview与布局文件中的不同。在chatfragment.xml文件中,您有:

  1. <androidx.recyclerview.widget.RecyclerView
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:id="@+id/recyclerview">
  5. </androidx.recyclerview.widget.RecyclerView>

在您的代码中,您正在使用:

  1. mRecyclerView = v.findViewById(R.id.recyclerView);

看到区别了吗?在布局文件中,id设置为 recyclerview 所有字母小写,而在代码中为 recyclerView 具有 W 大写,因此此行的nullpointerexception:

  1. mRecyclerView.setHasFixedSize(true);

要解决此问题,只需将上述代码行更改为:

  1. mRecyclerView = v.findViewById(R.id.recyclerview);
展开查看全部

相关问题