AsyncTask致命异常#1
我有一个使用通道将文本文件下载到本地文件的asynctask。AsyncTask致命异常#1
class getUrlsClass extends AsyncTask<String, Integer, File>{ @Override
protected File doInBackground(String... params) {
URLConnection uc;
File urlFiles= new File(getApplicationContext().getFilesDir(), "urls");
try {
URL url = new URL(params[0]);
uc = url.openConnection();
uc.setUseCaches(false);
ReadableByteChannel rbc = Channels.newChannel(uc.getInputStream());
FileOutputStream fos = new FileOutputStream(urlFiles);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
,我得到一个致命的运行时异常,会使得程序崩溃的开始:
07-22 01:31:01.769: E/AndroidRuntime(27610): FATAL EXCEPTION: AsyncTask #1 07-22 01:31:01.769: E/AndroidRuntime(27610): Process: com.example.griding, PID: 27610
07-22 01:31:01.769: E/AndroidRuntime(27610): java.lang.RuntimeException: An error occured while executing doInBackground()
07-22 01:31:01.769: E/AndroidRuntime(27610): at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-22 01:31:01.769: E/AndroidRuntime(27610): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-22 01:31:01.769: E/AndroidRuntime(27610): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-22 01:31:01.769: E/AndroidRuntime(27610): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-22 01:31:01.769: E/AndroidRuntime(27610): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-22 01:31:01.769: E/AndroidRuntime(27610): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-22 01:31:01.769: E/AndroidRuntime(27610): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-22 01:31:01.769: E/AndroidRuntime(27610): at java.lang.Thread.run(Thread.java:841)
07-22 01:31:01.769: E/AndroidRuntime(27610): Caused by: java.lang.IllegalArgumentException: position=0 count=9223372036854775807
07-22 01:31:01.769: E/AndroidRuntime(27610): at java.nio.FileChannelImpl.transferFrom(FileChannelImpl.java:368)
07-22 01:31:01.769: E/AndroidRuntime(27610): at com.example.myApp.DownloadManagerActivity$getUrlsClass.doInBackground(DownloadManagerActivity.java:284)
07-22 01:31:01.769: E/AndroidRuntime(27610): at com.example.myApp.DownloadManagerActivity$getUrlsClass.doInBackground(DownloadManagerActivity.java:1)
07-22 01:31:01.769: E/AndroidRuntime(27610): at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-22 01:31:01.769: E/AndroidRuntime(27610): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-22 01:31:01.769: E/AndroidRuntime(27610): ... 4 more
什么是任务的问题?
回答:
唯一的例外是在引起:
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
因为参数count
不能超过Integer.MAX_VALUE
更大。这是造成这种特殊的例外!
在FileChannelImpl
类文件发现这一点:
if (position < 0 || count < 0 || count > Integer.MAX_VALUE) { throw new IllegalArgumentException("position=" + position + " count=" + count);
}
希望它能帮助! :)
以上是 AsyncTask致命异常#1 的全部内容, 来源链接: utcz.com/qa/267126.html