2013-04-08 131 views
0

因此,我对此很新,而且我的应用程序不断崩溃。我在这里发现了这个代码,无法让它为我的生活工作。我想要做的只是下载选定的文件,并且当我不在作业文件夹的子目录中时它可以工作。我从MySQL数据库下载作业文件夹名称,并且工作正常。每个职业都有照片,文档,杂项等子......说我有工作4131 ..和4131里面有以下..异步任务致命异常 - 线程正在退出,未捕获的异常

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

和photots里面还有另一个png。 (4131 /图片/ dioghg.png)。问题是,如果我下载了基于4131方法的png wthin,它就起作用了。没有错误。如果我进入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(); 
     } 
    } 
} 

控制台输出

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 
+0

什么是第70行?有些东西是'空'的。可能'文件名' – codeMagic 2013-04-08 23:21:08

回答

2

这是您的罪魁祸首:

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

你传入nullinputstreamcopy(),这导致NPE。

+0

我已经在inputstreamcopy()被调用之前打印这两个值并且它们不为空... – JTester 2013-04-09 00:39:14

+0

我在零件中添加了这个: Log.v(“准备创建IS:”,“试图创建InputStream输入“); InputStream input = client.retrieveFileStream(srcFileSpec); Log.v(“IS Created:”,input.toString()); 当我尝试从子文件夹下载时,它甚至没有把它创建到IS创建(第3行),它只是崩溃。 – JTester 2013-04-09 00:48:19

相关问题