我正在为android制作文件管理器应用程序,并尝试使用AsyncTask实现复制和粘贴功能来复制文件。当我尝试粘贴项目时,应用程序崩溃并报告:
01-22 16:27:48.065: E/AndroidRuntime(8494): FATAL EXCEPTION: AsyncTask #1
01-22 16:27:48.065: E/AndroidRuntime(8494): Process: com.example.myapp, PID: 8494
01-22 16:27:48.065: E/AndroidRuntime(8494): java.lang.RuntimeException: An error occured while executing doInBackground()
01-22 16:27:48.065: E/AndroidRuntime(8494): at android.os.AsyncTask$3.done(AsyncTask.java:300)
01-22 16:27:48.065: E/AndroidRuntime(8494): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
01-22 16:27:48.065: E/AndroidRuntime(8494): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
01-22 16:27:48.065: E/AndroidRuntime(8494): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
01-22 16:27:48.065: E/AndroidRuntime(8494): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-22 16:27:48.065: E/AndroidRuntime(8494): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-22 16:27:48.065: E/AndroidRuntime(8494): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-22 16:27:48.065: E/AndroidRuntime(8494): at java.lang.Thread.run(Thread.java:841)
01-22 16:27:48.065: E/AndroidRuntime(8494): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
01-22 16:27:48.065: E/AndroidRuntime(8494): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
01-22 16:27:48.065: E/AndroidRuntime(8494): at java.util.ArrayList.get(ArrayList.java:308)
01-22 16:27:48.065: E/AndroidRuntime(8494): at com.example.myapp.MoveFiles.doInBackground(MoveFiles.java:30)
01-22 16:27:48.065: E/AndroidRuntime(8494): at com.example.myapp.MoveFiles.doInBackground(MoveFiles.java:1)
01-22 16:27:48.065: E/AndroidRuntime(8494): at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-22 16:27:48.065: E/AndroidRuntime(8494): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-22 16:27:48.065: E/AndroidRuntime(8494): ... 4 more
这是我的AsyncTask类:
public class MoveFiles extends AsyncTask<ArrayList<File>, Integer, Boolean> {
private ProgressDialog pd;
private String path;
public MoveFiles(ProgressDialog pd, String path) {
this.pd = pd;
this.path = path;
}
@Override
protected Boolean doInBackground(ArrayList<File>... params) {
ArrayList<File> files = params[0];
int count = files.size();
try {
for (int i = 0; i < count; i++) {
File destination = new File (path + "/" + files.get(i).getName());
copyDirectory(files.get(i), destination);
}
}
catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
protected void onPreExecute() {
pd.setMessage("Please wait..");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMax(100);
pd.setIndeterminate(false);
pd.setCancelable(true);
pd.show();
}
protected void onPostExecute(Boolean result) {
pd.dismiss();
}
protected void onProgressUpdate(Integer...progress){
}
static void copyDirectory(File sourceLocation, File targetLocation) throws IOException {
if (sourceLocation.isDirectory() && sourceLocation.exists()) {
if (!targetLocation.exists()) {
targetLocation.mkdir();
}
String[] children = sourceLocation.list();
for (int i = 0; i < children.length; i++) {
copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
}
} else {
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
}
if (DisplayDirectoryActivity.getCut()) {
DeleteRecursive(sourceLocation);
}
}
static void DeleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory())
for (File child : fileOrDirectory.listFiles())
DeleteRecursive(child);
fileOrDirectory.delete();
}
}
粘贴按钮的代码:
case R.id.context_accept_paste:
MoveFiles copy = new MoveFiles(new ProgressDialog(this), path);
copy.execute(filesMoving);
for (int i = 0; i < filesMoving.size(); i++)
currentFileList.add(path + "/" + filesMoving.get(i).getName());
selectedPaths.clear();
mAdapter.notifyDataSetChanged();
mode.finish(); // Action picked, so close the CAB
return true;
我知道这是OutOfBounds异常,但无法弄清楚为什么会一直存在。非常感谢任何帮助