0

如果目录包含任何图像但如果目录不包含图像 (0) 代码将爆炸并显示 java.lang.ArrayIndexOutOfBoundsException 错误,那么这是我的代码完美运行 ido 怎么办?我如何加载空白或空网格视图???这是我显示的图像http://tinypic.com/view.php?pic=2zyda9f&s=5 当我单击照片时(3)当我单击 hello(0)时工作正常 代码将在日志错误消息 java.lang 上爆炸显示.ArrayIndexOutOfBoundsException。

    GridView gridView;
  TextView textView;
File currentParent;
File[] currentFiles;
String[] currentFilePath;
boolean CheckArr[]; 
File root1;
LinearLayout ll;
String filePath;
String PhotosFolder = "Photos Folders";

SimpleAdapter simpleAdapter;

List<ImageData> imList;

 Button del;
 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

del = (Button) findViewById(R.id.canceles);
del.setOnClickListener(new OnClickListener() {      
    @Override
    public void onClick(View arg0) {

        for(int i=0;i<CheckArr.length;i++)
        {
            if(CheckArr[i] == true)
            {

                    File file = new 
  File(imList.get(i).getImgPath());
                    file.delete();
            }
        }


        inflateListView(currentFiles);
    }
 });


 ll = (LinearLayout) findViewById(R.id.linearlayout1);
 viewLoad = LayoutInflater.from(FileGridActivity.this).inflate(
        R.layout.main, null);
 AddPictures = (ImageButton) findViewById(R.id.add_privacy_contact);

  Addfolder = (Button) findViewById(R.id.Addfolder);

 textView = (TextView) findViewById(R.id.title_name);
 File photos = new File(getFilesDir(), "Photos");
 photos.mkdirs();

 root1 = new File("/data/data/com.myexample/files/");

 currentParent = root1;
 currentFiles = root1.listFiles();


 currentFilePath = new String[currentFiles.length];
 int count = 0;

 for (File f : currentFiles) {
    currentFilePath[count] = f.getAbsolutePath();
    count++;

 }

 gridView = (GridView) findViewById(R.id.grid);
 gridView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

        if (currentFiles[position].isDirectory() ) {


            root = new File("/data/data/com.myexample/files/"+ 
         FileName(currentFilePath[position]) + "/");

            currentFiles = root.listFiles();
            inflateListView(currentFiles);
        } else if (currentFiles[position].isFile()) {
            openFile(currentFiles[position]);

            // gridposition = currentFiles[(int) id]);

        }           }
   });


          private void inflateListView(File[] files) {

 imList = new ArrayList<ImageData>();
 ImageData imObj;

 List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();

 boolean check;

 for (int i = 0; i < files.length; i++) {
    Map<String, Object> listItem = new HashMap<String, Object>();





    if (files[i].isDirectory()) {





        listItem.put("icon", R.drawable.folder);
        listItem.put("fileName",files[i].getName() + "(" + 
    files[i].list().length + ")");

        listItems.add(listItem);

        simpleAdapter = new SimpleAdapter(this, listItems, 
   R.layout.line,new String[] { "icon", "fileName" }, new int[] { 
       R.id.icon,R.id.file_name });
    //  gridView.invalidateViews();
        gridView.setAdapter(simpleAdapter);

    //  simpleAdapter.notifyDataSetChanged();

        check = false;
    } 
    else 
    {   
        check = true;
        imObj = new ImageData();




        listItem.put("icon", files[i]);

        listItems.add(listItem);

        imObj.setImgPath(files[i].toString());

        imList.add(imObj);

        }


    if(check == true)
    {
        imageAdapter = new          


      ImagesAdapter(getApplicationContext(),FileGridActivity.this,0,imList);      
        gridView.setAdapter(imageAdapter);
        imageAdapter.notifyDataSetChanged();
    }
}



  }

  public class ImagesAdapter extends ArrayAdapter<ImageData> {

 private LayoutInflater mInflater;

 List<ImageData> listItems;

 public ImagesAdapter(Context context,Activity activity, int 
   textViewResourceId, List<ImageData> items) {
        super(context, textViewResourceId, items);
        this.listItems = items;
        mInflater = LayoutInflater.from(context);

        CheckArr = new boolean[items.size()];

        for(int z = 0; z<CheckArr.length;z++)
            CheckArr[z] = false;
 }

 public class ViewHolder{

       public CheckBox checkbox;
       public ImageView imageview;

 }

  public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;

            vi = mInflater.inflate(R.layout.galleryitem, null);


            ImageData o = listItems.get(position);

            if(o != null){
                holder = new ViewHolder();

                holder.imageview = (ImageView) 
  vi.findViewById(R.id.thumbImage);
                holder.checkbox = (CheckBox) 
vi.findViewById(R.id.itemCheckBox);

                File f = new 
  File(listItems.get(position).getImgPath());
                holder.imageview.setImageBitmap(decodeFile(f));

                holder.checkbox.setId(position);
                holder.imageview.setId(position);

                holder.checkbox.setOnCheckedChangeListener(new 
    OnCheckedChangeListener() {                 
                    public void 
   onCheckedChanged(CompoundButton arg0, boolean arg1) {
                        // TODO Auto-generated 
 method stub
                        Log.e("id",arg0.getId()+" 
  ");
                        Log.e("bool",arg1+ " ");

                        CheckArr[arg0.getId()] = 
    arg1;

                    }
                });

            }

        return vi;

    }   
4

1 回答 1

0

据我了解,您正在尝试访问不存在的索引。

这很简单

只需检查

在数组列表的情况下

yourarray.size();

或者在数组的情况下

yourarray.length;

如果返回的值大于 0,则执行:

photos(3); //whatever code you are using to retrieve data from the array

像这样的东西:

if(yourArray.getCount()>0){

//fetch the required data

}
else{

Log.v("TAG ","No data");

}
于 2013-05-02T08:48:13.277 回答