IndexOutOfBoundsException

IndexOutOfBoundsException

我有一个应用程序,可以将故事发布在具有视频和图像的轮播中。如果帖子是视频,则它带有声音开/关图标,如Instagram。当我从第一个视频转到第二个或第三个视频并单击声音开/关图标时,我的应用程序崩溃了。

下面是代码片段:

ArrayList<String> b_checkSound = new ArrayList<>();
cell_soundoff.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.setVolume(1, 1);
                cell_soundon.setVisibility(View.VISIBLE);
                cell_soundoff.setVisibility(View.GONE);
                if(b_checkSound != null) {
                    b_checkSound.add(position, "true");
                }
            }
        }
    }
});

cell_soundon.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.setVolume(0, 0);
                cell_soundoff.setVisibility(View.VISIBLE);
                cell_soundon.setVisibility(View.GONE);
                if (b_checkSound != null && b_checkSound.size() > position) {
                    b_checkSound.remove(position);
                }
            }
        }
    }
});


这是我得到的错误:

08-27 16:27:28.954 27154-27154/com.leadplatform.kfarmers E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.leadplatform.kfarmers, PID: 27154
    java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
        at java.util.ArrayList.add(ArrayList.java:483)
        at com.leadplatform.kfarmers.view.diary.DiaryListFragment$Stroy_imageviewAdapter$1.onClick(DiaryListFragment.java:2887)
        at android.view.View.performClick(View.java:6891)
        at android.view.View$PerformClick.run(View.java:26083)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6938)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

最佳答案

在这种情况下,请尝试使用HashMap,因为数组大小在您的情况下是不固定的,并且使用此方法,您不必担心IndexOutOfBoundsException异常:

        HashMap<Integer, String> b_checkSound = new HashMap<>(); //CHANGE HERE map with pos->value
        cell_soundoff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mediaPlayer != null) {
                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.setVolume(1, 1);
                        cell_soundon.setVisibility(View.VISIBLE);
                        cell_soundoff.setVisibility(View.GONE);
                        if(b_checkSound != null) {
                            b_checkSound.put(position, "true"); //CHANGE HERE
                        }
                    }
                }
            }
        });
//
        cell_soundon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mediaPlayer != null) {
                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.setVolume(0, 0);
                        cell_soundoff.setVisibility(View.VISIBLE);
                        cell_soundon.setVisibility(View.GONE);
                        if (b_checkSound != null) {//CHANGE HERE
                            b_checkSound.remove(position);
                        }
                    }
                }
            }
        });

关于android - java.lang.IndexOutOfBoundsException:索引:1,大小:0,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52034651/

10-10 09:32