我正在处理文件管理器。
我该如何解决这个问题?
我无法理解异常的原因。错误日志如下所示
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;
}
}
答案 0 :(得分:0)
directoryEntries
包含超过isFolders
的1个项目
directoryEntries.add("..");