因此,我正在创建一个网格视图。
本活动的想法是使用网格视图进入将要显示的目录,该视图将显示照片中的照片。假设正确显示了我将从网格视图中选择的图像
但是当我进入“活动”时,它崩溃了,并重新启动了应用程序
Logcat
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:411)
at com.example.dmckoy.myapplication.Search.GalleryFragment.setupGridView(GalleryFragment.java:133)
at com.example.dmckoy.myapplication.Search.GalleryFragment.access$100(GalleryFragment.java:34)
at com.example.dmckoy.myapplication.Search.GalleryFragment$3.onItemSelected(GalleryFragment.java:108)
这是错误3的来源:
第一个代码:
setImage(imgURLs.get(0), galleryImage, mAppend);
第二:
public class GalleryFragment extends Fragment {
private static final String TAG = "GalleryFragment";
第三:
setupGridView(directories.get(position));
这是整个代码:
public class GalleryFragment extends Fragment {
private static final String TAG = "GalleryFragment";
//constants
private static final int NUM_GRID_COLUMNS = 3;
//widgets
private GridView gridView;
private ImageView galleryImage;
private ProgressBar mProgressBar;
private Spinner directorySpinner;
//var
private ArrayList<String> directories;
private String mAppend = "file:/";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_gallery, container, false);
galleryImage = (ImageView) view.findViewById(R.id.galleryImageView);
gridView = (GridView) view.findViewById(R.id.gridView);
directorySpinner = (Spinner) view.findViewById(R.id.spinnerDirectory);
mProgressBar = (ProgressBar) view.findViewById(R.id.progressBar);
mProgressBar.setVisibility(View.GONE);
directories = new ArrayList<>();
Log.d(TAG, "onCreateView: started. ");
ImageView shareClose = (ImageView) view.findViewById(R.id.ivCloseShare);
shareClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: closing the gallery fragment. ");
getActivity().finish();
}
});
TextView nextScreen = (TextView) view.findViewById(R.id.tvNext);
nextScreen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: navigating to the final share screen. ");
}
});
init();
return view;
}
private void init() {
FilePath filePath = new FilePath();
//check for other folder inside "/storage/emulated/0/pictures
if (FileSearch.getDirectoryPaths(filePath.Picture) != null) {
directories = FileSearch.getDirectoryPaths(filePath.Picture);
}
directories.add(filePath.CAMERA);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, directories);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
directorySpinner.setAdapter(adapter);
directorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "onItemClick: selected. " + directories.get(position));
//setup our image grid for the directory chosen
setupGridView(directories.get(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void setupGridView(String selectedDirectory) {
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 image to gridview file://
GridImageAdapter adapter = new GridImageAdapter(getActivity(), R.layout.layout_grid_imageview, mAppend, imgURLs);
gridView.setAdapter(adapter);
//set the first image to be displayed whent the activity fragment view is inflated
setImage(imgURLs.get(0), galleryImage, mAppend);
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);
}
});
}
private void setImage(String imgURL, ImageView image, String append){
Log.d(TAG, "setImage: setting image");
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(append + imgURL, image, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
mProgressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
mProgressBar.setVisibility(View.INVISIBLE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
mProgressBar.setVisibility(View.INVISIBLE);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
mProgressBar.setVisibility(View.INVISIBLE);
}
});
}
}
最佳答案
java.lang.IndexOutOfBoundsException:索引:0,大小:0
您是using:
setImage(imgURLs.get(0), galleryImage, mAppend);
这意味着
imgURLs
具有0个元素。在使用
imgURLs.get(0)
之前,只需检查它是否具有第一个元素,例如:if (imgURLs != null && imgURLs.size()>=1) { ... }