【问题标题】:java.lang.IndexOutOfBoundsException in fileManager文件管理器中的 java.lang.IndexOutOfBoundsException
【发布时间】:2013-07-15 17:17:07
【问题描述】:

我正在开发文件管理器。

我该如何解决这个问题?

我无法理解异常的原因。错误日志如下所示

ERROR/AndroidRuntime(6155): FATAL EXCEPTION: main
    java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
    at java.util.ArrayList.get(ArrayList.java:304)
    at com.FileExplorer.FileSystemAdapter.getView(FileSystemAdapter.java:46)

这里是 MainActivity

public class MainActivity extends Activity
{
    FileSystemAdapter adapter;
    ListView lv;
    File currentDirectory ;
    TextView textTitle ;
    ArrayList<String> directoryEntries;
    ArrayList<Integer> isFolders ;
    public final static int yesFolder = 20;
    public final static int noFolder = 30;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textTitle = (TextView)findViewById(R.id.textTitle);
        lv = (ListView)findViewById(R.id.listView);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView <?> parent, View view, int position, long id)
            {
                String selectedFileString = directoryEntries.get(position);
                if(selectedFileString.equals(".."))
                    upOneLevel();
                else
                {
                    File clickedFile = new File(selectedFileString);
                    browseTo(clickedFile);
                }
            }
        });
        browseTo(Environment.getExternalStorageDirectory());
    }
    private void browseTo(File directory)
    {
        if(directory.isDirectory())
        {
            currentDirectory = directory;
            fill(directory.listFiles());
            textTitle.setText(directory.getAbsolutePath());
        }
        else
        {
            Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("file://" + directory.getAbsolutePath()));
            startActivity(intent);
        }
    }
    private void fill(File[] files)
    {
        if(files == null)
        {
            Toast.makeText(this,"fill empty argument ",Toast.LENGTH_LONG).show();
            return ;
        }
        directoryEntries  = new ArrayList<String>();
        isFolders = new ArrayList<Integer>();

        if(currentDirectory.getParent() != null)
        directoryEntries.add("..");
        for(File file : files)
        {
            directoryEntries.add(file.getAbsolutePath());
            if(file.isDirectory())
                isFolders.add(yesFolder);
            else
                isFolders.add(noFolder);
        }
        adapter = new FileSystemAdapter(this,directoryEntries,isFolders);
        lv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
    private void upOneLevel()
    {
        if(currentDirectory.getParent() != null)
        {
            browseTo(currentDirectory.getParentFile());
        }
    }
}

这是 BaseAdapter 类

public class FileSystemAdapter extends BaseAdapter
{
    LayoutInflater inflater;
    ArrayList<String> files;
    ArrayList<Integer> isFolder;
    Context ctx;
    public FileSystemAdapter(Context ctx ,ArrayList<String> files,ArrayList<Integer> isFolder)
    {
        this.files = files;
        this.ctx = ctx;
        this.isFolder = isFolder;
        inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount()
    {
        return files.size();
    }
    @Override
    public Object getItem(int position)
    {
        return files.get(position);
    }
    @Override
    public long getItemId(int position)
    {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View v = inflater.inflate(R.layout.item,parent,false);
        ImageView imageFolder = (ImageView)v.findViewById(R.id.imageFolder);
        if(isFolder.get(position) == MainActivity.yesFolder)
        {
            imageFolder.setImageResource(R.drawable.folder);
        }
        TextView textItem = (TextView)v.findViewById(R.id.textItem);
        textItem.setText(files.get(position));
        return v;
    }
}

【问题讨论】:

  • 第 46 行是哪一个?

标签: java android file-management


【解决方案1】:

directoryEntries 包含比 isFolders 多 1 项,因为

directoryEntries.add("..");

【讨论】:

  • 我看了一整天的代码,没看到这个小麻烦,非常感谢。我还有一个问题,如何对 directoryEntries 进行排序,以免在 directoryEntries 和 isFolders 之间失去绑定; (对不起我的英语不好)
  • 您可以有一个File 列表并在getView 中测试isDirectory。
猜你喜欢
  • 2017-11-18
  • 2013-09-29
  • 1970-01-01
  • 2012-06-30
  • 1970-01-01
  • 1970-01-01
  • 2022-06-30
  • 2011-09-01
  • 2014-04-02
相关资源
最近更新 更多