异步任务FATAL EXCEPTION - 线程正在退出,未捕获异常

时间:2013-04-08 23:13:40

标签: java android android-asynctask

所以我对此很新,我的应用程序一直在崩溃。我在这里找到了这个代码,无法让它为我的生活工作。我想要做的只是下载所选文件,当我不在作业文件夹的子目录中时,它可以正常工作。我从MySQL数据库下载作业文件夹名称,并且工作正常。每个工作都有照片,文档,misc等子文件夹...说我有工作4131 ..而在4131里面有以下内容..

4131 / cdgjkg.png 4131 /杂项 4131 /照片

在照片里面还有另外一个png。 (4131 /图片/ dioghg.png)。问题是,如果我下载png wthin基础4131方法,它的工作原理。没有错误。如果我进入Photots目录并尝试下载该文件,我会收到大量的异步错误。下面是代码。如果您需要了解更多信息,请与我们联系。

new LoadAllJobs().execute();

    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String file = ((TextView)view).getText().toString();
            String current_dir = dir + "/" +  file;
            if(file.contains(".")){
                //download the file or view it
                try {
                    Log.d("Downloading ", file);
                    new FTPDownload().execute(file);  // <-----This is where I start the task to enter the FTPDownload, never to be seen again :(
                    Log.d("Success ", current_dir);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }

这是一个块,我只检索列表并下载文件,只要它包含'。' (文件夹名称中的服务器上不允许使用'。'字符,因此这是一个快速作弊行为。)

FTPDownload类

class FTPDownload extends AsyncTask<String, Integer, String> {
//public FTPClient client;

@Override
protected String doInBackground(String... filename) {
    try {
        System.out.println("Filename->: " + filename[0]);
        GetFileFTP("/" + filename[0], Environment.getExternalStorageDirectory() + "/Download/", filename[0]);
    }
    finally
    {
        //e.printStackTrace();
        Log.v("DONE: ", "All done!");
    }
    System.out.println("I HATE YOU.");  // <----- NEVER REACHES THIS LINE WHEN TRYING TO DOWNLOAD IN A SUBFOLDER
    return null;
}



    public void GetFileFTP(String srcFileSpec, String destpath, String destname) {
    FTPClient client = new FTPClient();
    Log.v("pathSpec: ", destpath);

    try {
        client.connect("xxxxxx");
        client.login("xxxxxx", "xxxxxx");

        client.enterLocalPassiveMode();
        client.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);

        InputStream input = client.retrieveFileStream(srcFileSpec);
        if(input != null) Log.v("GetFileFTP: ", "INPUT STREAM OPENED SUCCESSFULLY!");
        Log.v("File InputStream: ", srcFileSpec);

        File output_file = new File(destpath + destname);
        Log.v("OUTPUTFILE: ", destpath + destname);

        inputstreamcopy(input, output_file);

        /*try {
            //Log.e("Closing connection: ", client.toString());
            client.logout();
            client.disconnect();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
    }
    catch (IOException e) {
        Log.e("FTP", "Error Getting File");
        e.printStackTrace();
    }
}

    public static void inputstreamcopy(InputStream source, File destination){
        try {
            org.apache.commons.io.FileUtils.copyInputStreamToFile(source, destination);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

CONSOLE OUTPUT

04-08 15:10:05.010: D/Downloading(29309): login.php
04-08 15:10:05.010: D/Success(29309): 4131/Miscellaneous/login.php
04-08 15:10:05.010: I/System.out(29309): Filename->: login.php
04-08 15:10:05.020: V/pathSpec:(29309): /storage/emulated/0/Download/
04-08 15:10:06.010: V/File InputStream:(29309): /login.php
04-08 15:10:06.020: V/OUTPUTFILE:(29309): /storage/emulated/0/Download/login.php
04-08 15:10:06.030: V/DONE:(29309): All done!
04-08 15:10:06.030: W/dalvikvm(29309): threadid=11: thread exiting with uncaught exception (group=0x418f5930)
04-08 15:10:06.040: E/AndroidRuntime(29309): FATAL EXCEPTION: AsyncTask #1
04-08 15:10:06.040: E/AndroidRuntime(29309): java.lang.RuntimeException: An error occured while executing doInBackground()
04-08 15:10:06.040: E/AndroidRuntime(29309):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at java.lang.Thread.run(Thread.java:856)
04-08 15:10:06.040: E/AndroidRuntime(29309): Caused by: java.lang.NullPointerException
04-08 15:10:06.040: E/AndroidRuntime(29309):    at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1792)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1769)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at org.apache.commons.io.IOUtils.copy(IOUtils.java:1744)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at org.apache.commons.io.FileUtils.copyInputStreamToFile(FileUtils.java:1512)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at com.example.tdsi.FTPDownload.inputstreamcopy(FTPDownload.java:70)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at com.example.tdsi.FTPDownload.GetFileFTP(FTPDownload.java:51)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at com.example.tdsi.FTPDownload.doInBackground(FTPDownload.java:21)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at com.example.tdsi.FTPDownload.doInBackground(FTPDownload.java:1)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-08 15:10:06.040: E/AndroidRuntime(29309):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-08 15:10:06.040: E/AndroidRuntime(29309):    ... 4 more

1 个答案:

答案 0 :(得分:2)

这是你的罪魁祸首:

04-08 15:10:06.040: E/AndroidRuntime(29309): Caused by: java.lang.NullPointerException
[...]
org.apache.commons.io.FileUtils.copyInputStreamToFile(FileUtils.java:1512)

您正在将null传递给inputstreamcopy(),这会导致NPE。