我无法在FirestoreRecylerView中看到我的数据。将startListening添加到适配器时,我得到了NPE。我没有使用onCreate方法。我读了一个类似问题的答案,其中建议使用onCreate。但是,我认为我不需要onCreate。有什么办法可以解决这个问题?
这是我的片段代码,我想在其中查看回收站:
package com.example.XX;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.List;
public class MainQueryFragment extends Fragment {
RecyclerView recyclerView;
ProfileRecyclerAdapter ProfileRecyclerAdapter;
TextView yourSector;
TextView yourPincode;
FirebaseFirestore db = FirebaseFirestore.getInstance();
String tuserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
String customerPincode;
String customerSector;
String sectorText;
String pincodeText;
String[] testArray;
String name;
String pincode;
String address;
String mobileNumber;
String sector;
String avgDeliveryTime;
String avgDeliveryTimeUrg;
public MainQueryFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_main_query, container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
yourSector = view.findViewById(R.id.Sector_tv);
yourPincode = view.findViewById(R.id.Pincode_tv);
recyclerView = view.findViewById(R.id.recylerView_Profile);
DocumentReference docRef = db.collection("customerUsers").document(tuserID);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
customerPincode = document.getString("pincode");
customerSector = document.getString("sector");
sectorText = "Sector : " + customerSector;
pincodeText = "Pincode : " + customerPincode;
yourSector.setText(sectorText);
yourPincode.setText(pincodeText);
} else {
Log.d("docref", "No such document");
}
} else {
Log.d("docref", "get failed with ", task.getException());
}
}
});
Button button = view.findViewById(R.id.startQuery_button);
if (getArguments() != null) {
MainQueryFragmentArgs args = MainQueryFragmentArgs.fromBundle(getArguments());
testArray = args.getArrayCheckboxData();
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Query query = FirebaseFirestore.getInstance()
.collection("tUsers")
.whereEqualTo("pincode", customerPincode)
.whereEqualTo("sector", customerSector)
.whereEqualTo(testArray[0], true)
.whereEqualTo(testArray[1], true)
.whereEqualTo(testArray[2], true)
.whereEqualTo(testArray[3], true);
FirestoreRecyclerOptions<Profile> options = new FirestoreRecyclerOptions.Builder<Profile>()
.setQuery(query,Profile.class)
.build();
ProfileRecyclerAdapter = new ProfileRecyclerAdapter(options);
recyclerView.setAdapter(ProfileRecyclerAdapter);
}
});
}
@Override
public void onStart() {
super.onStart();
ProfileRecyclerAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
ProfileRecyclerAdapter.stopListening();
}
}
这是我的适配器类:
package com.example.XX;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
public class ProfileRecyclerAdapter extends FirestoreRecyclerAdapter<Profile,ProfileRecyclerAdapter.ProfileViewHolder> {
public ProfileRecyclerAdapter(@NonNull FirestoreRecyclerOptions<Profile> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull ProfileViewHolder holder, int position, @NonNull Profile profile) {
holder.Name_textView.setText(profile.getName());
holder.Sector_textView.setText(profile.getSector());
holder.Pincode_textView.setText(profile.getPincode());
}
@NonNull
@Override
public ProfileViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
View view=layoutInflater.inflate(R.layout.profile_row_item,parent,false);
return new ProfileViewHolder(view);
}
class ProfileViewHolder extends RecyclerView.ViewHolder{
TextView Name_textView;
TextView Sector_textView;
TextView Pincode_textView;
public ProfileViewHolder(@NonNull View itemView) {
super(itemView);
Name_textView=itemView.findViewById(R.id.Name_textView);
Sector_textView=itemView.findViewById(R.id.Sector_textView);
Pincode_textView=itemView.findViewById(R.id.Pincode_textView);
}
}
}
我的日志:
2020-06-14 12:17:53.043 32238-32238/com.example.XX E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.XX, PID: 32238
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.XX.ProfileRecyclerAdapter.startListening()' on a null object reference
at com.example.XX.MainQueryFragment.onStart(MainQueryFragment.java:172)
at androidx.fragment.app.Fragment.performStart(Fragment.java:2730)
at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:365)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1194)
at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
编辑:这是我的最新尝试,但仍未显示任何内容。我仍然得到E/RecyclerView: No adapter attached; skipping layout
,我相信我已经在onCreateView中添加了空适配器。 :
public class MainQueryFragment extends Fragment {
RecyclerView recyclerView;
ProfileRecyclerAdapter ProfileRecyclerAdapter;
TextView yourSector;
TextView yourPincode;
FirebaseFirestore db = FirebaseFirestore.getInstance();
String tuserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
String customerPincode;
String customerSector;
String sectorText;
String pincodeText;
String[] testArray;
String name;
String pincode;
String address;
String mobileNumber;
String sector;
String avgDeliveryTime;
String avgDeliveryTimeUrg;
public MainQueryFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_main_query, container, false);
recyclerView =view.findViewById(R.id.recyclerView_Profile);
recyclerView.setAdapter(ProfileRecyclerAdapter);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
yourSector = view.findViewById(R.id.Sector_tv);
yourPincode = view.findViewById(R.id.Pincode_tv);
recyclerView = view.findViewById(R.id.recylerView_Profile);
DocumentReference docRef = db.collection("customerUsers").document(tuserID);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
customerPincode = document.getString("pincode");
customerSector = document.getString("sector");
sectorText = "Sector : " + customerSector;
pincodeText = "Pincode : " + customerPincode;
yourSector.setText(sectorText);
yourPincode.setText(pincodeText);
} else {
Log.d("docref", "No such document");
}
} else {
Log.d("docref", "get failed with ", task.getException());
}
}
});
Button button = view.findViewById(R.id.startQuery_button);
if (getArguments() != null) {
MainQueryFragmentArgs args = MainQueryFragmentArgs.fromBundle(getArguments());
testArray = args.getArrayCheckboxData();
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Query query = FirebaseFirestore.getInstance()
.collection("tUsers")
.whereEqualTo("pincode", customerPincode)
.whereEqualTo("sector", customerSector)
.whereEqualTo(testArray[0], true)
.whereEqualTo(testArray[1], true)
.whereEqualTo(testArray[2], true)
.whereEqualTo(testArray[3], true);
FirestoreRecyclerOptions<Profile> options = new FirestoreRecyclerOptions.Builder<Profile>()
.setQuery(query,Profile.class)
.build();
ProfileRecyclerAdapter = new ProfileRecyclerAdapter(options);
recyclerView.setAdapter(ProfileRecyclerAdapter);
ProfileRecyclerAdapter.startListening();
}
});
}
}
如果在onCreate中添加recyclerView.setAdapter(ProfileRecyclerAdapter);
,我会得到NPE!
您正在onClickListener
内部初始化适配器,因此,当您运行该应用程序并调用所有生命周期时,由于ProfileRecyclerAdapter ProfileRecyclerAdapter;
等于null,因此将出现此异常。您需要将startListening()
移到clickListener
内:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Query query = FirebaseFirestore.getInstance()
.collection("tUsers")
.whereEqualTo("pincode", customerPincode)
.whereEqualTo("sector", customerSector)
.whereEqualTo(testArray[0], true)
.whereEqualTo(testArray[1], true)
.whereEqualTo(testArray[2], true)
.whereEqualTo(testArray[3], true);
FirestoreRecyclerOptions<Profile> options = new FirestoreRecyclerOptions.Builder<Profile>()
.setQuery(query,Profile.class)
.build();
ProfileRecyclerAdapter = new ProfileRecyclerAdapter(options);
recyclerView.setAdapter(ProfileRecyclerAdapter);
ProfileRecyclerAdapter.startListening();
}
});