【问题标题】:Crashing when pressing back button - Android [duplicate]按下后退按钮时崩溃 - Android [重复]
【发布时间】:2019-07-25 14:39:06
【问题描述】:

有一个活动显示所有使用RecyclerView 的用户。我有一个简单的搜索代码。我通过测试一些代码弄清楚了自己。它使用 Firestore 查询工作。就是这样:

allUsersSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            firebaseFirestore.collection("Users").orderBy("username").startAt(s.toString()).endAt(s.toString() + "\uf8ff").addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

                    usersList.clear();

                    for (DocumentChange documentChange: queryDocumentSnapshots.getDocumentChanges()) {

                        if (documentChange.getType() == DocumentChange.Type.ADDED) {

                            String user_id = documentChange.getDocument().getId();
                            Users users = documentChange.getDocument().toObject(Users.class).withId(user_id);
                            usersList.add(users);

                            allUsersRecyclerAdapter.notifyDataSetChanged();

                        }

                    }

                }
            });

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

但每当我按下设备的后退按钮或工具栏的后退按钮时,应用程序就会崩溃。这就是我在 LogCat 中得到的:

com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.app, PID: 4502
    java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.set(ArrayList.java:427)
    at com.example.app.AllUsersActivity$2.onEvent(AllUsersActivity.java:136)
    at com.example.app.AllUsersActivity$2.onEvent(AllUsersActivity.java:114)
    at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2(com.google.firebase:firebase-firestore@@20.1.0:915)
    at com.google.firebase.firestore.Query$$Lambda$3.onEvent(com.google.firebase:firebase-firestore@@20.1.0)
    at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0(com.google.firebase:firebase-firestore@@20.1.0:42)
    at com.google.firebase.firestore.core.AsyncEventListener$$Lambda$1.run(com.google.firebase:firebase-firestore@@20.1.0)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6126)
    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)

这是加载用户的方式:

usersList.clear();

    Query allUsersQuery = firebaseFirestore.collection("Users").orderBy("online", Query.Direction.DESCENDING);
    allUsersQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

            for (DocumentChange documentChange: queryDocumentSnapshots.getDocumentChanges()) {

                if (documentChange.getType() == DocumentChange.Type.ADDED) {

                    String user_id = documentChange.getDocument().getId();
                    Users users = documentChange.getDocument().toObject(Users.class).withId(user_id);
                    usersList.add(users);

                    allUsersRecyclerAdapter.notifyDataSetChanged();

                } else if (documentChange.getType() == DocumentChange.Type.MODIFIED) {

                    String user_id = documentChange.getDocument().getId();
                    Users users = documentChange.getDocument().toObject(Users.class).withId(user_id);

                    if (documentChange.getOldIndex() == documentChange.getNewIndex()) {

                        // Item changed but remained in same position
                        usersList.set(documentChange.getOldIndex(), users);
                        allUsersRecyclerAdapter.notifyItemChanged(documentChange.getOldIndex());

                    } else {

                        // Item changed and changed position
                        usersList.remove(documentChange.getOldIndex());
                        usersList.add(documentChange.getNewIndex(), users);
                        allUsersRecyclerAdapter.notifyItemMoved(documentChange.getOldIndex(),documentChange.getNewIndex());
                    }

                    allUsersRecyclerAdapter.notifyDataSetChanged();

                } else if (documentChange.getType() == DocumentChange.Type.REMOVED) {

                    usersList.remove(documentChange.getOldIndex());
                    allUsersRecyclerAdapter.notifyItemRemoved(documentChange.getOldIndex());

                }

            }

        }
    });

这一行at com.example.app.AllUsersActivity$2.onEvent(AllUsersActivity.java:136) 指这一行usersList.set(documentChange.getOldIndex(), users); 并且这一行at com.example.app.AllUsersActivity$2.onEvent(AllUsersActivity.java:114) 指这一行allUsersQuery.addSnapshotListener(new EventListener&lt;QuerySnapshot&gt;() {

有谁知道问题出在哪里?

【问题讨论】:

    标签: android google-cloud-firestore android-recyclerview


    【解决方案1】:

    你可以使用 onBackPressed 方法。

     @Override
        public void onBackPressed() {
            super.onBackPressed();
            startActivity(new Intent(this,HomePage.class));
        }
    

    【讨论】:

      【解决方案2】:

      根据您的问题,很明显您正在对空白 userList 进行设置,因此您需要像这样检查条件,

        if(userList.size==0){
          userList.set(0,users);
          }else{
          usersList.set(documentChange.getOldIndex(), users);
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-29
        • 2020-04-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多