第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從被調(diào)用方傳遞的參數(shù),但在方法定義中為 null

從被調(diào)用方傳遞的參數(shù),但在方法定義中為 null

有只小跳蛙 2021-09-15 14:25:30
我基本上是在嘗試創(chuàng)建一個(gè)解壓縮功能。我在下面的塊中使用參數(shù)調(diào)用了該函數(shù):UnzipUtility unzipUtility = new UnzipUtility();    try {        unzipUtility.unzip(localFilePath, parentPath);    } catch (IOException e) {        e.printStackTrace();    }該方法定義在一個(gè)UnzipUtility類中,代碼如下:    public void unzip(String zipFilePath, String destDirectory) throws IOException {    File destDir = new File(destDirectory);    if (!destDir.exists()) {        destDir.mkdir();    }    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));    ZipEntry entry = zipIn.getNextEntry();    // iterates over entries in the zip file    while (entry != null) {        String filePath = destDirectory + File.separator + entry.getName();        if (!entry.isDirectory()) {            // if the entry is a file, extracts it            extractFile(zipIn, filePath);        } else {            // if the entry is a directory, make the directory            File dir = new File(filePath);            dir.mkdir();        }        zipIn.closeEntry();        entry = zipIn.getNextEntry();    }    zipIn.close();}但是在運(yùn)行時(shí),雖然參數(shù)在主類中正確傳遞,但值在 unzip 方法中顯示為 null。請(qǐng)幫忙解決這個(gè)問(wèn)題
查看完整描述

2 回答

?
眼眸繁星

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊

可以剪下以下幾行:


UnzipUtility unzipUtility = new UnzipUtility();

try {

    unzipUtility.unzip(localFilePath, parentPath);

} catch (IOException e) {

    e.printStackTrace();

}

在類SFTPActivity 中粘貼以下行的正下方:


Downloader(fileName); 

的方法doInBackground。實(shí)際上,該方法doInBackground()在與該方法運(yùn)行的線程不同的線程中onCreate運(yùn)行。您嘗試在方法Downloader(fileName)完成其工作之前使用變量。這就是您在變量中看到空值的原因,例如:localFilePath和parentPath


查看完整回答
反對(duì) 回復(fù) 2021-09-15
?
慕神8447489

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊

您的代碼不起作用的原因是因?yàn)樵谶\(yùn)行此行時(shí):


unzipUtility.unzip(localFilePath, parentPath);

變量localFilePath和parentPath尚未設(shè)置。


您可能會(huì)爭(zhēng)辯說(shuō),它們是在方法中設(shè)置的,該方法Downloader在該unzip行之前調(diào)用。不幸的是,事實(shí)并非如此。在這種情況下,代碼執(zhí)行不是線性的,因?yàn)槟褂玫氖茿syncTask. 異步任務(wù)中的內(nèi)容與其后面的行同時(shí)運(yùn)行。


您的下載調(diào)用不會(huì)在調(diào)用之前完成unzipUtility.unzip,因?yàn)榕c創(chuàng)建新UnzipUtility對(duì)象相比,下載需要大量時(shí)間。


這就是為什么localFilePath并且parentPath為空。


解決此問(wèn)題的一種方法是將解壓縮邏輯也移動(dòng)到異步任務(wù)中:


new AsyncTask<Void, Void, List<String>>() {

    @Override

    protected List<String> doInBackground(Void... params) {

        try {

            Downloader(fileName);


            UnzipUtility unzipUtility = new UnzipUtility();

            unzipUtility.unzip(localFilePath, parentPath);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;

    }


}.execute();

另一種方式是重寫onPostExecute的AsyncTask還有:


new AsyncTask<Void, Void, List<String>>() {

     // doInBackground goes here...


     @Override

     protected void onPostExecute(Long result) {

         try {

            UnzipUtility unzipUtility = new UnzipUtility();

            unzipUtility.unzip(localFilePath, parentPath);

         } catch (Exception e) {

            e.printStackTrace();

         }

     }

}


查看完整回答
反對(duì) 回復(fù) 2021-09-15
  • 2 回答
  • 0 關(guān)注
  • 233 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)