我正在尝试使用来自我的firebase数据库的数据来填充我的firebase recyclerview中的数据。我正在使用“查询”功能来过滤结果但我从“查询”获取空指针异常错误当我是运行应用程序,但“查询”没有从调试返回“null”。这是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.users_list);
users2 = (RecyclerView) findViewById(R.id.users_list);
users2.setHasFixedSize(true);
users2.setLayoutManager(new LinearLayoutManager(this));
firebaseAuth = FirebaseAuth.getInstance();
databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
final String user_id = firebaseAuth.getCurrentUser().getUid();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot){
halls2 = (String) dataSnapshot.child("Male").child(user_id).child("halls").getValue();
halls1 = (String) dataSnapshot.child("Female").child(user_id).child("halls").getValue();
if(dataSnapshot.child("Male").hasChild(user_id)){
}
else{
query = databaseReference.child("Male").orderByChild("halls").equalTo(halls1);
}
}
@Override
public void onCancelled(DatabaseError databaseError){
}
});
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot){
if(dataSnapshot.child("Male").hasChild(user_id)){
query = databaseReference.child("Female").orderByChild("halls").equalTo(halls2);
}
else{
query = databaseReference.child("Male").orderByChild("halls").equalTo(halls1);
}
}
@Override
public void onCancelled(DatabaseError databaseError){
}
});
FirebaseRecyclerAdapter<User, UserViewholder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<User, UserViewholder>(
User.class, R.layout.users_row, UserViewholder.class, query) {
@Override
protected void populateViewHolder(UserViewholder viewHolder, User model, int position) {
viewHolder.setUsername(model.getUsername());
viewHolder.setLevels(model.getLevels());
viewHolder.setDownloadUrl(getApplicationContext(), model.getDownloadUrl());
}
};
users2.setAdapter(firebaseRecyclerAdapter);
数据库结构
{
"Users" : {
"Female" : {
"WwBjblOo8maSP5PXlYviONrS3Oo2" : {
"downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2Fecf2758a-5628-4297-bcb3-e8c3f138cbbd.jpeg?alt=media&token=1ca6067d-6c08-49b9-a2bf-6a8315ada067",
"halls" : "Kwapong",
"levels" : "300",
"username" : "merylstre"
},
"X8c80zaY0kfT5KVoTFHbhPIEG9z2" : {
"downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2F51dc26a1-45e8-4897-9c2f-14caf8e79cc5.jpeg?alt=media&token=90818c1e-167e-4799-bc29-b3fe52529cd2",
"halls" : "Sarbah",
"levels" : "100",
"username" : "hashcodef"
}
},
"Male" : {
"2EXejmHoMlWoKSDb5APvSt8do5e2" : {
"downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2F83ca18a0-63a5-477a-ab1e-b2732b3ebfa1.jpeg?alt=media&token=b8626221-a67c-48e3-b958-d373c9f63c69",
"halls" : "Sarbah",
"levels" : "100",
"username" : "fillman"
},
"BW79oGosAIhdxDxYXbtHbSbC0D23" : {
"downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2F90b93b19-230b-4f28-b394-358d0c516c5c.jpeg?alt=media&token=0c82c272-d6fb-46e8-a6cf-581f5501de7b",
"halls" : "Sarbah",
"levels" : "100",
"username" : "radjames"
},
"Mop4hbS0zne6QS3woum1udLgwqf1" : {
"downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2F78621a8b-7d67-413c-9e57-f378e60ba41e.jpeg?alt=media&token=3982ee0a-2a34-41b3-9250-f0884531ee86",
"halls" : "Sarbah",
"levels" : "100",
"username" : "radjohn"
},
"VvnOY3k7Cwa36bJW55JiZDJRwPN2" : {
"downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2F1ac7080f-8db8-49a7-bb76-34b007deb839.jpeg?alt=media&token=6d100527-80d3-49b3-b0ae-617dd55e64de",
"halls" : "Sarbah",
"levels" : "100",
"username" : "ciryadams"
},
"WOT11DVkoVb9owzK4qKHbvT4ODF2" : {
"downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2F3fda02ae-c577-4a4a-86c1-cadcaf518c97.jpeg?alt=media&token=5baf2a79-b14c-4e28-b7bb-77e55ae08e55",
"halls" : "Sarbah",
"levels" : "100",
"username" : "markzuck"
},
"dbnmFoC4hnSCsXsxJgVO0Q7GYx13" : {
"downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2F2366c787-fb16-4049-9c7d-3d0c2a09ab9b.jpeg?alt=media&token=25bda8dd-ab24-4b9b-8299-534e49bf9ed4",
"halls" : "Sarbah",
"levels" : "100",
"username" : "dasdsada"
},
"iqZfSgGNpPZ4HDtOUuHYZ1u8x273" : {
"downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2Fc52cd467-05b9-4ece-8085-4cdf502839c5.jpeg?alt=media&token=4c9eb36e-4644-49ec-85eb-c2fef38ca50e",
"halls" : "Sarbah",
"levels" : "100",
"username" : "maameEsi"
},
"jj9yycmDNbZwiPVTLrRyYZheM702" : {
"downloadUrl" : "https://firebasestorage.googleapis.com/v0/b/loginapp-8088d.appspot.com/o/userProfiles%2F5c4a34b2-bc7b-43e7-8b00-7e0bf63ff252.jpeg?alt=media&token=59a11905-a67d-48ea-9f16-10ffd3915876",
"halls" : "Sarbah",
"levels" : "100",
"username" : "hashcode"
}
}
}
}
错误日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: dev.mintz.loginapp, PID: 3157
java.lang.RuntimeException: Unable to start activity ComponentInfo{dev.mintz.loginapp/dev.mintz.loginapp.Users}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference
at com.firebase.ui.database.FirebaseArray.<init>(FirebaseArray.java:41)
at com.firebase.ui.database.FirebaseRecyclerAdapter.<init>(FirebaseRecyclerAdapter.java:91)
at dev.mintz.loginapp.Users$2.<init>(Users.java:0)
at dev.mintz.loginapp.Users.onCreate(Users.java:108)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:1)
您的new FirebaseRecyclerAdapter
方法也可能在users2.setLayoutManager
之后立即生效。
仅仅因为某些代码行之间的代码为query =
,并不表示它运行的时间 - 特别是因为Firebase是异步的。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.users_list);
firebaseAuth = FirebaseAuth.getInstance();
databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
users2 = (RecyclerView) findViewById(R.id.users_list);
users2.setHasFixedSize(true);
users2.setLayoutManager(new LinearLayoutManager(this));
// query = ... ; // Set this to literally anything but 'null'
// Otherwise, your error is here...
final FirebaseRecyclerAdapter<User, UserViewholder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<User, UserViewholder>(
User.class, R.layout.users_row, UserViewholder.class, query) {
@Override
protected void populateViewHolder(UserViewholder viewHolder, User model, int position) {
viewHolder.setUsername(model.getUsername());
viewHolder.setLevels(model.getLevels());
viewHolder.setDownloadUrl(getApplicationContext(), model.getDownloadUrl());
}
};
users2.setAdapter(firebaseRecyclerAdapter);
// The rest of your code
databaseReference.addValueEventListener(new ValueEventListener() {
...
然后,在代码的其余部分中,您可以更新query
,但必须通知适配器查询已更改。
public void onDataChange(...) {
...
query = ... ; // Update query
firebaseRecyclerAdapter.notifyDataSetChanged(); // Update UI
}