我正在创建一个应用程序,它将许多用户及其客户端保存在firebase数据库中。我有相关客户端的用户,我想显示当前用户的客户端列表,但是当我提示显示客户端时,应用程序崩溃。我不知道这是由于错误的代码还是客户端的布局。
这一点
userClients.addListenerForSingleValueEvent(new ValueEventListener() {
程序不喜欢ValueEventListener。
任何帮助都将不胜感激。
public class ClientList extends AppCompatActivity {
String userId;
FirebaseAuth mAuth;
FirebaseUser firebaseUser;
DatabaseReference databaseRef;
String clientAddress;
String clientPhone;
ListView myClientList;
List<String> clientArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_list);
mAuth = FirebaseAuth.getInstance();
myClientList = (ListView) findViewById(R.id.clientList);
clientArrayList = new ArrayList<>();
getClientList();
}
public void getClientList(){
databaseRef = FirebaseDatabase.getInstance().getReference();
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser!=null){
userId = firebaseUser.getUid();
}
Query userClients = databaseRef.child("users").child(userId);
userClients.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Iterate through the friends JSON tree (for current user)
for (DataSnapshot myDatabase : dataSnapshot.getChildren()) {
clientAddress = myDatabase.child("Address").getValue(String.class);
clientPhone = myDatabase.child("phone").getValue(String.class);
clientArrayList.add( clientAddress + ", " + clientPhone );
}
createListView();
if (clientArrayList.isEmpty()) {
Toast.makeText(ClientList.this, "No Clients", Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void createListView() {
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, getResources().getStringArray(Integer.parseInt(userId)));
myClientList.setAdapter(adapter);
}
ClientList}: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
答案 0 :(得分:0)
如果没有登录用户,firebaseUser
将为空,并且不会设置userId
,使其保留为空的默认值。这将导致使用null参数调用child()
,这是异常的原因:
public void getClientList(){
databaseRef = FirebaseDatabase.getInstance().getReference();
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser!=null){
userId = firebaseUser.getUid();
}
Query userClients = databaseRef.child("users").child(userId); // userId == null here
...
}
答案 1 :(得分:0)
试试这个:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference userRef = database.getReference("users").child(user.getUid());//DatabaseReference, no Query
告诉它是否有效
答案 2 :(得分:0)
This is because user might not have signed in and hence 0x42
is null.
You can do the following to resolve the same.
0x41