我正在尝试完成我的 DownloadFile 类,它是 AsyncTask 的子类。所以这是类的代码:
private class DownloadFile extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... sUrl)
{
try
{
URL url = new URL(sUrl[0]);
URLConnection urlc = url.openConnection();
urlc.connect();
int fileLength = urlc.getContentLength();
InputStream is = new BufferedInputStream(url.openStream());
OutputStream os = new FileOutputStream(Environment.getDataDirectory().getAbsolutePath().concat("data/com.markosoft.maturko/databases/proba.mp3"));
byte Data[] = new byte[1024];
long total = 0;
int count;
while((count=is.read(Data))!=-1)
{
total+=count;
publishProgress((int)total * 100 / fileLength);
os.write(Data,0,count);
}
os.flush();
os.close();
is.close();
}
catch(Exception e)
{
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress)
{
dl.setProgress(progress[0]);
}
@Override
protected void onPostExecute(String unused)
{
dl.dismiss();
Toast.makeText(getActivity(), "Podaci uspešno preuzeti", Toast.LENGTH_SHORT).show();
}
}
我收到 NullPointerException 错误和“线程退出未捕获的异常”错误。当我添加显示下载已完成的 Toast 调用时发生此错误,但据我所知 onPostExecute 是在 UI 线程上执行的,所以我没有看到问题。让我烦恼的另一件事是下载没有完成,或者根本没有开始。当我调用调用 DownloadFile.execute(link) 函数的函数时,onPostExecute 函数会立即激活...感谢您的帮助
编辑:dl 是在调用 dlf.execute(link) 之前初始化的 ProgressDialog,因此这不应该是 NullPointerException 的原因...编辑:这是请求的 LogCat 日志:
03-22 19:56:51.119: W/dalvikvm(546): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
03-22 19:56:51.140: E/AndroidRuntime(546): FATAL EXCEPTION: main
03-22 19:56:51.140: E/AndroidRuntime(546): java.lang.NullPointerException
03-22 19:56:51.140: E/AndroidRuntime(546): at android.widget.Toast.<init>(Toast.java:92)
03-22 19:56:51.140: E/AndroidRuntime(546): at android.widget.Toast.makeText(Toast.java:233)
03-22 19:56:51.140: E/AndroidRuntime(546): at com.markosoft.maturko.Upit$DownloadFile.onPostExecute(Upit.java:68)
03-22 19:56:51.140: E/AndroidRuntime(546): at com.markosoft.maturko.Upit$DownloadFile.onPostExecute(Upit.java:1)
03-22 19:56:51.140: E/AndroidRuntime(546): at android.os.AsyncTask.finish(AsyncTask.java:602)
03-22 19:56:51.140: E/AndroidRuntime(546): at android.os.AsyncTask.access$600(AsyncTask.java:156)
03-22 19:56:51.140: E/AndroidRuntime(546): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
03-22 19:56:51.140: E/AndroidRuntime(546): at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 19:56:51.140: E/AndroidRuntime(546): at android.os.Looper.loop(Looper.java:137)
03-22 19:56:51.140: E/AndroidRuntime(546): at android.app.ActivityThread.main(ActivityThread.java:4340)
03-22 19:56:51.140: E/AndroidRuntime(546): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 19:56:51.140: E/AndroidRuntime(546): at java.lang.reflect.Method.invoke(Method.java:511)
03-22 19:56:51.140: E/AndroidRuntime(546): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-22 19:56:51.140: E/AndroidRuntime(546): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-22 19:56:51.140: E/AndroidRuntime(546): at dalvik.system.NativeStart.main(Native Method)
或者如果您更愿意在 Notepad++ 中查看它,请点击以下链接: Log