【问题标题】:Android app crashing when displaying firebase database entries显示 Firebase 数据库条目时 Android 应用程序崩溃
【发布时间】:2017-08-30 18:21:07
【问题描述】:

我正在创建一个应用程序,它将在 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()

【问题讨论】:

  • 你的日志说什么?发布堆栈跟踪
  • 错误是什么?
  • 你能把你的错误日志或logcat放在这里吗?
  • 我编辑了我的帖子以包含错误。它说 NullPointer ...不能在 child 中传递 null。

标签: java android firebase firebase-realtime-database event-listener


【解决方案1】:

当没有登录用户时,firebaseUser 将为空,userId 将不会被设置,而将其保留为空默认值。这将导致 child() 使用 null 参数调用,这是异常的原因:

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
    ...
}

【讨论】:

  • 我已经创建了一个用户,并且我已经使用该用户成功登录。那么为什么 userId == null 呢?
  • @RacEgan:添加一些调试日志以确认 firebaseUser != null
  • /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 初始化失败。
  • @RacEgan:您可以尝试进行清理和重建,尽管这可能无济于事。您可能需要发布您的应用模块 build.gradle,以便我们查看您正在构建的库和版本。
  • 谢谢鲍勃。我将 10.0.1 库用于核心、数据库、crash、auth、play-service-auth 和消息传递............appcompat-v7 和 design=25.3.1。和 firebase-client-android = 2.5.2。它们都相互兼容。
【解决方案2】:

试试这个:

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

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

并告诉它是否有效

【讨论】:

【解决方案3】:

这是因为用户可能尚未登录,因此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");
                }
            }
        };
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-26
    • 2014-12-30
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多