我正在尝试完成我的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
答案 0 :(得分:3)
http://developer.android.com/reference/android/os/AsyncTask.html。请查看标题 4个步骤下的主题。
您正在获取Toast的NullPointerException。如果你从MainActivity调用asynctask那么
尝试
Toast.makeText(MainActivity.this, "Podaci uspešno preuzeti", Toast.LENGTH_SHORT).show();
您应该将活动上下文,文本和持续时间作为参数传递给makeText。
OR
根据Eng.Fouad的建议,你的dl为空。
答案 1 :(得分:1)
我怀疑这是因为dl
是null
,所以在第一次调用onProgressUpdate()
时,NullPointerException
会被{{1}中的catch-bock抛出并抓住}}
接下来,调用doInBackground()
并再次抛出onPostExecute()
,但这次它会传播到应用程序的android主启动器。