stacktrace E / UncaughtException:java.lang.IndexOutOfBoundsException:索引1无效

时间:2017-12-01 20:02:47

标签: java android indexoutofboundsexception leaderboard

我正在开发一个带有firebase排行榜的Android应用程序,它运行良好,但是当我点击排行榜时崩溃, 这是堆栈跟踪日志,  我收到2个错误:

E / UncaughtException:java.lang.IndexOutOfBoundsException:索引1无效,大小为1

E / AndroidRuntime:FATAL EXCEPTION:main

12-02 01:05:53.969 3508-3508/com.example.myapp E/UncaughtException: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
                                                                            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                                            at java.util.ArrayList.get(ArrayList.java:308)
                                                                            at com.example.myapp.activity.LeaderboardActivity$4.onChildAdded(LeaderboardActivity.java:167)
                                                                            at com.google.android.gms.internal.zzaer.zza(Unknown Source)
                                                                            at com.google.android.gms.internal.zzagp.zzSu(Unknown Source)
                                                                            at com.google.android.gms.internal.zzags$1.run(Unknown Source)
                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            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)
12-02 01:05:54.215 3508-3508/com.example.myapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.example.myapp, PID: 3508
                                                                     java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
                                                                         at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                                         at java.util.ArrayList.get(ArrayList.java:308)
                                                                         at com.examle.myapp.activity.LeaderboardActivity$4.onChildAdded(LeaderboardActivity.java:167)
                                                                         at com.google.android.gms.internal.zzaer.zza(Unknown Source)
                                                                         at com.google.android.gms.internal.zzagp.zzSu(Unknown Source)
                                                                         at com.google.android.gms.internal.zzags$1.run(Unknown Source)
                                                                         at android.os.Handler.handleCallback(Handler.java:739)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         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)

蓝线表示leaderboardactivity.java中此行的错误

if (leaderboards.get(1).getName().equalsIgnoreCase(displayName)) {

这是来自leaderboardactivity.java的代码片段

private void getLeaderboard() {

        databaseLeaderboard.orderByChild("score").limitToLast((int) totalValue).addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
                Leaderboard leaderboard = dataSnapshot.getValue(Leaderboard.class);
                leaderboards.add(leaderboard);

                if (leaderboards.size() >= (int) totalValue) {
                    Collections.reverse(leaderboards);
                    leaderboardAdapterNew.notifyDataSetChanged();
                }

                if (leaderboards.size() == totalValue) {


                    if (leaderboards.get(0).getName().equalsIgnoreCase(displayName)) {
                        txtUname1.setText("Me");
                    } else {
                        txtUname1.setText(leaderboards.get(0).getName());
                    }
                    if (leaderboards.get(1).getName().equalsIgnoreCase(displayName)) {
                        txtUname2.setText("Me");
                    } else {
                        txtUname2.setText(leaderboards.get(1).getName());
                    }
                    if (leaderboards.get(2).getName().equalsIgnoreCase(displayName)) {
                        txtUname3.setText("Me");
                    } else {
                        txtUname3.setText(leaderboards.get(2).getName());
                    }

有人能告诉我我的错吗?

1 个答案:

答案 0 :(得分:1)

是的,您正在尝试访问不存在的索引:

改变这个:

if (leaderboards.size() == totalValue) {

对此:

if (leaderboards.size() == totalValue && totalValue > 2) {