ArrayIndexOutOfBounds甚至在异常处理Android之后也是例外

时间:2018-04-30 04:08:22

标签: java android exception-handling file-handling

所以我在片段中填充网格视图,并且我有一个微调器,其中列出了文件夹选项

This is the Gallery Fragment with the spinner

每当我选择没有文件的文件夹时,应用程序崩溃就会产生ArrayIndexOutOfBounds异常

FATAL EXCEPTION: main
                         Process: manika.aditya.ekayana, PID: 13405
                                                                   java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
                                                                       at java.util.ArrayList.get(ArrayList.java:411)
                                                                       at manika.aditya.ekayana.Share.GalleryFragment.setupGridView(GalleryFragment.java:170)
                                                                       at manika.aditya.ekayana.Share.GalleryFragment.access$300(GalleryFragment.java:37)
                                                                       at manika.aditya.ekayana.Share.GalleryFragment$3.onItemSelected(GalleryFragment.java:142)

这是我编写的用于填充GridView

的函数

正如您所看到的,它会抛出一个ArrayIndexOutOfBounds Exception

private void setupGridView(String selectedDirectory) throws ArrayIndexOutOfBoundsException {
    Log.d(TAG, "setupGridView: directory chosen: " + selectedDirectory);
    final ArrayList<String> imgURLs = FileSearch.getFilePaths(selectedDirectory);

    //set the grid column width
    int gridWidth = getResources().getDisplayMetrics().widthPixels;
    int imageWidth = gridWidth/NUM_GRID_COLUMNS;
    gridView.setColumnWidth(imageWidth);

    //use the grid adapter to adapter the images to gridview
    GridImageAdapter adapter = new GridImageAdapter(getActivity(), R.layout.layout_grid_imageview, mAppend, imgURLs);
    gridView.setAdapter(adapter);

    //set the first image to be displayed when the activity fragment view is inflated

    try{

        setImage(imgURLs.get(0), galleryImage, mAppend);

    }

    catch (ArrayIndexOutOfBoundsException aioobe){}

    mSelectedImage = imgURLs.get(0);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d(TAG, "onItemClick: selected an image: " + imgURLs.get(position));

            setImage(imgURLs.get(position), galleryImage, mAppend);
            mSelectedImage = imgURLs.get(position);
        }
    });

}

甚至函数调用也包含在try和catch块中,该块再次捕获ArrayIndexOutOfBounds异常

try{

                setupGridView(directories.get(position));
            }

            catch (ArrayIndexOutOfBoundsException aioobe)  { }

        }

这是目录/文件搜索方法,以防万一

directories = new ArrayList<>();

文件路径功能

 public static ArrayList<String> getFilePaths(String directory){
    ArrayList<String> pathArray = new ArrayList<>();
    File file = new File(directory);
    File[] listfiles = file.listFiles();
    for(int i = 0; i < listfiles.length; i++){
        if(listfiles[i].isFile()){
            pathArray.add(listfiles[i].getAbsolutePath());
        }
    }
    return pathArray;
}

}

和文件路径类

public class FilePaths {

//"storage/emulated/0"
public String ROOT_DIR = Environment.getExternalStorageDirectory().getPath();

public String PICTURES = ROOT_DIR + "/Pictures";
public String CAMERA = ROOT_DIR + "/DCIM/camera";

public String FIREBASE_IMAGE_STORAGE = "photos/users/";

}

2 个答案:

答案 0 :(得分:1)

尝试更改此

try{

    setImage(imgURLs.get(0), galleryImage, mAppend);

}

catch (ArrayIndexOutOfBoundsException aioobe){}

mSelectedImage = imgURLs.get(0);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.d(TAG, "onItemClick: selected an image: " + imgURLs.get(position));

        setImage(imgURLs.get(position), galleryImage, mAppend);
        mSelectedImage = imgURLs.get(position);
    }
});

进入这个

try{
    setImage(imgURLs.get(0), galleryImage, mAppend);
    mSelectedImage = imgURLs.get(0);
}
catch (ArrayIndexOutOfBoundsException aioobe){}

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        try {
            Log.d(TAG, "onItemClick: selected an image: " + imgURLs.get(position));
            setImage(imgURLs.get(position), galleryImage, mAppend);
            mSelectedImage = imgURLs.get(position);
        } 
        catch (ArrayIndexOutOfBoundsException aioobe){}
    }
});

答案 1 :(得分:0)

在从imgURLS获取索引之前,检查imgURLS中有一些值。

if(imgURLs.size()>0){              
    try{


                        setImage(imgURLs.get(0), galleryImage, mAppend);



                    mSelectedImage = imgURLs.get(0);
                    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Log.d(TAG, "onItemClick: selected an image: " + imgURLs.get(position));

                            setImage(imgURLs.get(position), galleryImage, mAppend);
                            mSelectedImage = imgURLs.get(position);
                        }
                    });

             catch (Exception EX){
            }
    }
else{
//No image url
}

}