2017-08-30 83 views
0

我正在创建一个应用程序,该应用程序会将一些用户及其客户端保存在Firebase数据库中。我有相关客户端的用户,我想显示当前用户的客户端列表,但当提示显示客户端时,应用程序崩溃。我不知道这是由于不正确的代码或客户端的布局。显示Firebase数据库条目时Android应用程序崩溃

在这一点上

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

你说什么?帖子栈跟踪 – DroiDev

+0

什么是错误? –

+0

你可以把你的错误日志或logcat放在这里吗? –

回答

0

当没有登录的用户,firebaseUser将是无效和userId不会设置,其空默认值离开它。这将导致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 
    ... 
} 
+0

我已经创建了一个用户,并且我已经与该用户成功登录。那为什么userId == null? –

+0

@RacEgan:添加一些调试日志记录以确认'firebaseUser!= null'。 –

+0

/data/app/com.example.rachel.finalyearproject-2/split_lib_dependencies_apk.apk:classes15.dex中的com.google.android.gms.internal.zzatp​​类验证失败,因为:验证程序拒绝了com.google.android类。 gms.internal.zzatp​​由于错误的方法com.google.android.gms.internal.zzaso com.google.android.gms.internal.zzatp​​.zzJg() 08-30 20:05:50.012 7535-7535/com。 example.rachel.finalyearproject A/FirebaseApp:Firebase API初始化失败..com.google.android.gms.internal.zzatp​​.zzJg() Firebase API初始化失败。 –

0

试试这个:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); 
    FirebaseDatabase database = FirebaseDatabase.getInstance(); 

    DatabaseReference userRef = database.getReference("users").child(user.getUid());//DatabaseReference, no Query 

而且告诉我们,如果它的工作原理

+0

没有工作。但感谢您输入 –

+0

没问题。你确定你为认证过程正确编写代码吗?检查我的答案在这里https://stackoverflow.com/questions/45967649/firebase-authentication-error-in-android/45967699#45967699 – Razvan

0

这是因为用户可能尚未登录,因此userId为空。 您可以执行以下操作来解决此问题。

public class ClientList extends AppCompatActivity { 

    String userId; 

    FirebaseAuth mAuth; 
    FirebaseUser firebaseUser; 
    DatabaseReference databaseRef; 
    String clientAddress; 
    String clientPhone; 
    ListView myClientList; 
    List<String> clientArrayList; 

    // 1. Add AuthStateListener 
    private FirebaseAuth.AuthStateListener mAuthListener; 

    // 3.add and remove AuthStateListener on Activity's onStart and onStop respectively 
    @Override 
    protected void onStart() { 
     super.onStart(); 
     mAuth.addAuthStateListener(mAuthListener); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     mAuth.removeAuthStateListener(mAuthListener); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_client_list); 


     mAuth = FirebaseAuth.getInstance(); 

     // 2. Init the AuthStateListener 
     mAuthListener = new FirebaseAuth.AuthStateListener() { 
      @Override 
      public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
       /* 
        Everytime a user is signed in/out, this will be called. 
        (Including when the activity launches) 
       */ 

       if (firebaseAuth.getCurrentUser() != null) { 
        // User signed in, now get client-list 
        getClientList(); 
       } else { 
        /* No user signed in, First sign in. After having signed in, again this method will be called (since the auth-state has changed). Then the if-case runs and getClientList() is called 
        */ 
        mAuth.signInWithEmailAndPassword("user_email", "user_password"); 
       } 
      } 
     }; 
    } 
相关问题