我試圖完成我的DownloadFile類,它是AsyncTask的子類。 所以這是類的代碼:onPostExecute上的NullException void
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(鏈接)函數的函數時,onPostExecute函數會立即激活...感謝您的幫助
編輯:dl是在調用dlf.execute(鏈接)之前初始化的ProgressDialog,不應該是空指針異常的原因... 編輯:這裏的要求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)
,或者如果你寧願看它在記事本++這裏的鏈接: Log
請寄出你所有的Logcat錯誤,你在哪裏定義'dl'? – Sam 2013-03-22 18:40:18
確定試圖表明舉杯爲:'Toast.makeText(Your_Current_Fragment.this.getActivity(), 「Podaciuspešnopreuzeti」,Toast.LENGTH_SHORT).show();' – 2013-03-22 18:53:47
我不認爲你可以調用'getActivity() '在AsyncTask上可以嗎?我可能是錯的... – Grambot 2013-03-22 18:54:11