显示Firebase数据库条目时Android应用程序崩溃 [英] Android app crashing when displaying firebase database entries

查看:74
本文介绍了显示Firebase数据库条目时Android应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,该应用程序将在Firebase数据库中容纳大量用户及其客户端.我的用户具有相关的客户端,并且想显示当前用户的客户端列表,但是当我提示显示客户端时,应用程序崩溃.我不知道这是由于错误的代码或客户端的布局造成的.

I'm creating an application which will hold a number of users and their clients in a firebase database. I have users with relevant clients and I want to display the client list for the current user, but when I prompt the displaying of the clients, the application crashes. I don't know if it's due to incorrect code or the layout of the clients.

这一点

 userClients.addListenerForSingleValueEvent(new ValueEventListener() {

程序不喜欢ValueEventListener.

the program does not like the 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()

推荐答案

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

This is because user might not have signed in and hence userId is null. You can do the following to resolve the same.

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");
                }
            }
        };
    }

这篇关于显示Firebase数据库条目时Android应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆