gpt4 book ai didi

java - 当我尝试从 Firestore 获取数据并显示在 ListView fragment 中时,我的应用程序崩溃了

转载 作者:行者123 更新时间:2023-12-02 01:34:40 25 4
gpt4 key购买 nike

当我尝试从 firestore 访问数据并将其显示在 ListView fragment 类中时,我的应用程序崩溃了。任何帮助都会对我有益

availablestudent_fragment.java(主 fragment 类)

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.firebase.firestore.DocumentChange;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QuerySnapshot;
import com.google.firebase.firestore.auth.User;

import java.util.ArrayList;
import java.util.List;

public class availablestudent_fragment extends Fragment {

private static final String TAG ="FireLog" ;
private RecyclerView nmainlist;
private FirebaseFirestore mfirestore;
private List<Users> usersList;
private UserListAdapter userListAdapter;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_availablestudents,null);

nmainlist=(RecyclerView)v.findViewById(R.id.main_list);
nmainlist.setHasFixedSize(true);
nmainlist.setLayoutManager(new LinearLayoutManager(getContext()));
nmainlist.setAdapter(userListAdapter);
mfirestore=FirebaseFirestore.getInstance();

usersList=new ArrayList<>();
userListAdapter=new UserListAdapter(usersList);


mfirestore.collection("users").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.d(TAG, "Error" + e.getMessage());
}

for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {


if (doc.getType() == DocumentChange.Type.ADDED) {
Users users = doc.getDocument().toObject(Users.class);
usersList.add(users);
userListAdapter.notifyDataSetChanged();
}
}

}
});
return v;
}
}

users.java(模型类)

    package com.kiit.projectmanager;

public class Users {

String Student_Name,Roll_Number;

public Users(){

}

public Users(String student_Name, String roll_Number) {
this.Student_Name = student_Name;
this.Roll_Number = roll_Number;
}

public String getStudent_Name() {
return Student_Name;
}

public void setStudent_Name(String student_Name) {
this.Student_Name = student_Name;
}

public String getRoll_Number() {
return Roll_Number;
}

public void setRoll_Number(String roll_Number) {
this.Roll_Number = roll_Number;
}


}

UsersListAdapter.java

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.firebase.firestore.auth.User;

import org.w3c.dom.Text;

import java.util.List;

public class UserListAdapter extends RecyclerView.Adapter<UserListAdapter.ViewHolder> {
public List<Users> usersList;

public UserListAdapter(List<Users> usersList){
this.usersList=usersList;
}


@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

holder.StudentName.setText(usersList.get(position).getStudent_Name());
holder.RollNumber.setText(usersList.get(position).getRoll_Number());

}

@Override
public int getItemCount() {
return usersList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

View mview;
public TextView StudentName;
public TextView RollNumber;


public ViewHolder(@NonNull View itemView) {
super(itemView);
mview=itemView;

StudentName=(TextView)mview.findViewById(R.id.name);
RollNumber=(TextView)mview.findViewById(R.id.rollnumber);
}
}
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="13dp"
android:padding="13dp">

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/mirza_semibold"

android:text="NAME"
android:textColor="@color/common_google_signin_btn_text_dark_focused"
android:textSize="18dp" />

<TextView
android:id="@+id/rollnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/fjalla_one"
android:text="ROLLNUMBER"
android:textColor="@color/common_google_signin_btn_text_dark_focused"
android:textSize="14dp" />

</LinearLayout>

Fragment_availablestudents.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/main_list"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

</LinearLayout>

logcat_Error(当我尝试单击我的 fragment 时,我的应用程序崩溃了)

    03-29 19:28:58.838 18481-18677/com.kiit.projectmanager I/OpenGLRenderer: Initialized EGL, version 1.4
03-29 19:28:58.876 18481-18481/com.kiit.projectmanager W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
03-29 19:28:58.956 18481-18677/com.kiit.projectmanager E/HAL: hw_get_module_by_class: module name gralloc
03-29 19:28:58.956 18481-18677/com.kiit.projectmanager E/HAL: hw_get_module_by_class: module name gralloc
03-29 19:28:59.054 18481-18638/com.kiit.projectmanager D/FA: Connected to remote service
03-29 19:28:59.900 18481-18481/com.kiit.projectmanager I/Timeline: Timeline: Activity_launch_request time:50369167
03-29 19:28:59.920 18481-18638/com.kiit.projectmanager D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=1162, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=9078248192392117239}]
03-29 19:29:00.229 18481-18638/com.kiit.projectmanager D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=MainActivity, firebase_previous_id(_pi)=9078248192392117239, firebase_screen_class(_sc)=login_activity, firebase_screen_id(_si)=9078248192392117240}]
03-29 19:29:02.730 18481-18481/com.kiit.projectmanager I/Choreographer: Skipped 146 frames! The application may be doing too much work on its main thread.
03-29 19:29:04.698 18481-18481/com.kiit.projectmanager I/Choreographer: Skipped 117 frames! The application may be doing too much work on its main thread.
03-29 19:29:04.705 18481-18481/com.kiit.projectmanager I/Timeline: Timeline: Activity_launch_request time:50373972
03-29 19:29:04.724 18481-18638/com.kiit.projectmanager D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=4492, firebase_screen_class(_sc)=login_activity, firebase_screen_id(_si)=9078248192392117240}]
03-29 19:29:04.763 18481-18638/com.kiit.projectmanager D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=login_activity, firebase_previous_id(_pi)=9078248192392117240, firebase_screen_class(_sc)=student_login, firebase_screen_id(_si)=9078248192392117241}]
03-29 19:29:07.105 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:07.105 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:10.300 18481-18638/com.kiit.projectmanager D/FA: Setting user property (FE): _sid, 1553867950
03-29 19:29:10.341 18481-18638/com.kiit.projectmanager D/FA: Logging event (FE): session_start(_s), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=student_login, firebase_screen_id(_si)=9078248192392117241, session_id(_sid)=1553867950}]
03-29 19:29:10.376 18481-18638/com.kiit.projectmanager D/FA: Connected to remote service
03-29 19:29:11.005 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:11.005 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:11.666 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:11.666 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:15.494 18481-18481/com.kiit.projectmanager W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
03-29 19:29:15.497 18481-18481/com.kiit.projectmanager W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
03-29 19:29:15.499 18481-18481/com.kiit.projectmanager W/IInputConnectionWrapper: setComposingRegion on inactive InputConnection
03-29 19:29:19.178 18481-18481/com.kiit.projectmanager W/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzal@8cbf476
03-29 19:29:20.161 18481-18481/com.kiit.projectmanager I/Toast: Show toast from OpPackageName:com.kiit.projectmanager, PackageName:com.kiit.projectmanager
03-29 19:29:24.288 18481-18481/com.kiit.projectmanager W/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzal@8cbf476
03-29 19:29:24.781 18481-18481/com.kiit.projectmanager I/Toast: Show toast from OpPackageName:com.kiit.projectmanager, PackageName:com.kiit.projectmanager
03-29 19:29:29.533 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:29.533 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:30.249 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:30.250 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:30.320 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:30.320 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:32.446 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:32.446 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:32.769 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:32.769 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:36.362 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:36.362 18481-18481/com.kiit.projectmanager E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-29 19:29:42.405 18481-18481/com.kiit.projectmanager W/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzal@8cbf476
03-29 19:29:44.406 18481-18583/com.kiit.projectmanager D/FirebaseAuth: Notifying id token listeners about user ( yBhPtTpYDdQgviffTlUvhLwAOPn1 ).
03-29 19:29:44.420 18481-18481/com.kiit.projectmanager D/FirebaseApp: Notifying auth state listeners.
03-29 19:29:44.420 18481-18481/com.kiit.projectmanager D/FirebaseApp: Notified 0 auth state listeners.
03-29 19:29:44.437 18481-18481/com.kiit.projectmanager I/Timeline: Timeline: Activity_launch_request time:50413704
03-29 19:29:44.475 18481-18638/com.kiit.projectmanager D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=39712, firebase_screen_class(_sc)=student_login, firebase_screen_id(_si)=9078248192392117241}]
03-29 19:29:44.646 18481-19899/com.kiit.projectmanager W/DynamiteModule: Local module descriptor class for providerinstaller not found.
03-29 19:29:44.652 18481-18638/com.kiit.projectmanager D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=student_login, firebase_previous_id(_pi)=9078248192392117241, firebase_screen_class(_sc)=student_activity, firebase_screen_id(_si)=9078248192392117242}]
03-29 19:29:44.655 18481-19899/com.kiit.projectmanager I/DynamiteModule: Considering local module providerinstaller:0 and remote module providerinstaller:0
03-29 19:29:44.655 18481-19899/com.kiit.projectmanager W/ProviderInstaller: Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
03-29 19:29:44.656 18481-19899/com.kiit.projectmanager W/ResourceType: No package identifier when getting name for resource number 0x00000000
03-29 19:29:44.817 18481-18638/com.kiit.projectmanager D/FA: Connected to remote service
03-29 19:29:44.884 18481-19899/com.kiit.projectmanager I/art: Rejecting re-init on previously-failed class java.lang.Class<com.google.android.gms.org.conscrypt.Java7ExtendedSSLSession>
03-29 19:29:44.885 18481-19899/com.kiit.projectmanager I/art: Rejecting re-init on previously-failed class java.lang.Class<com.google.android.gms.org.conscrypt.Java7ExtendedSSLSession>
03-29 19:29:44.888 18481-19899/com.kiit.projectmanager I/art: Rejecting re-init on previously-failed class java.lang.Class<com.google.android.gms.org.conscrypt.Java8ExtendedSSLSession>
03-29 19:29:44.889 18481-19899/com.kiit.projectmanager I/art: Rejecting re-init on previously-failed class java.lang.Class<com.google.android.gms.org.conscrypt.Java8ExtendedSSLSession>
03-29 19:29:44.946 18481-19899/com.kiit.projectmanager I/ProviderInstaller: Installed default security provider GmsCore_OpenSSL
03-29 19:29:45.890 18481-18481/com.kiit.projectmanager W/PathParser: Points are too far apart 4.000000596046461
03-29 19:29:48.115 18481-18481/com.kiit.projectmanager E/RecyclerView: No adapter attached; skipping layout
03-29 19:29:48.134 18481-18481/com.kiit.projectmanager W/PathParser: Points are too far apart 4.000000596046461
03-29 19:29:48.467 18481-18481/com.kiit.projectmanager D/AndroidRuntime: Shutting down VM
03-29 19:29:48.468 18481-18481/com.kiit.projectmanager E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kiit.projectmanager, PID: 18481
java.lang.RuntimeException: Found two getters or fields with conflicting case sensitivity for property: roll_number
at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.addProperty(com.google.firebase:firebase-firestore@@18.1.0:670)
at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.<init>(com.google.firebase:firebase-firestore@@18.1.0:586)
at com.google.firebase.firestore.util.CustomClassMapper.loadOrCreateBeanMapperForClass(com.google.firebase:firebase-firestore@@18.1.0:348)
at com.google.firebase.firestore.util.CustomClassMapper.convertBean(com.google.firebase:firebase-firestore@@18.1.0:502)
at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-firestore@@18.1.0:243)
at com.google.firebase.firestore.util.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-firestore@@18.1.0:97)
at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.1.0:203)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.1.0:121)
at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.1.0:183)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.1.0:101)
at com.kiit.projectmanager.availablestudent_fragment$1.onEvent(availablestudent_fragment.java:59)
at com.kiit.projectmanager.availablestudent_fragment$1.onEvent(availablestudent_fragment.java:48)
at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2(com.google.firebase:firebase-firestore@@18.1.0:891)
at com.google.firebase.firestore.Query$$Lambda$3.onEvent(com.google.firebase:firebase-firestore@@18.1.0)
at com.google.firebase.firestore.util.ExecutorEventListener.lambda$onEvent$0(com.google.firebase:firebase-firestore@@18.1.0:42)
at com.google.firebase.firestore.util.ExecutorEventListener$$Lambda$1.run(com.google.firebase:firebase-firestore@@18.1.0)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
03-29 19:29:58.480 18481-19899/com.kiit.projectmanager W/Firestore: (18.1.0) [OnlineStateTracker]: Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds

This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

从单元格检索错误以使其更清晰

   java.lang.RuntimeException: Found two getters or fields with conflicting case sensitivity for property: roll_number
at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.addProperty(com.google.firebase:firebase-firestore@@18.1.0:670)
at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.<init>(com.google.firebase:firebase-firestore@@18.1.0:586)
at com.google.firebase.firestore.util.CustomClassMapper.loadOrCreateBeanMapperForClass(com.google.firebase:firebase-firestore@@18.1.0:348)
at com.google.firebase.firestore.util.CustomClassMapper.convertBean(com.google.firebase:firebase-firestore@@18.1.0:502)
at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-firestore@@18.1.0:243)
at com.google.firebase.firestore.util.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-firestore@@18.1.0:97)
at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.1.0:203)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.1.0:121)
at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.1.0:183)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.1.0:101)
at com.kiit.projectmanager.availablestudent_fragment$1.onEvent(availablestudent_fragment.java:59)
at com.kiit.projectmanager.availablestudent_fragment$1.onEvent(availablestudent_fragment.java:48)
at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2(com.google.firebase:firebase-firestore@@18.1.0:891)
at com.google.firebase.firestore.Query$$Lambda$3.onEvent(com.google.firebase:firebase-firestore@@18.1.0)
at com.google.firebase.firestore.util.ExecutorEventListener.lambda$onEvent$0(com.google.firebase:firebase-firestore@@18.1.0:42)
at com.google.firebase.firestore.util.ExecutorEventListener$$Lambda$1.run(com.google.firebase:firebase-firestore@@18.1.0)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)

最佳答案

要解决此问题,您需要根据 Java Naming Conventions 修复 Users 类中字段和方法的大小写。 。所以你的模型类应该是这样的:

public class Users {
String studentName, rollNumber;

public Users(){}

public Users(String studentName, String rollNumber) {
this.studentName = studentName;
this.rollNumber = rollNumber;
}

public String getStudentName() {
return studentName;
}

public void setStudentName(String studentName) {
this.studentName = studentName;
}

public String getRollNumber() {
return rollNumber;
}

public void setRollNumber(String rollNumber) {
this.rollNumber = rollNumber;
}
}

删除旧数据,添加新数据,一切都会正常工作。

关于java - 当我尝试从 Firestore 获取数据并显示在 ListView fragment 中时,我的应用程序崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55419244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com