2012-06-12 112 views
0

我有一個關於Android中的AsyncTask類的問題,以及它爲什麼給我一個錯誤。我已經定義了一個內部類在這裏..Android的AsyncTask錯誤與onPostExecute

class MyTask extends AsyncTask<String,String,Integer>{ 


      Context context; 
       ProgressDialog dialog; 
       MyTask(Context c) 
       { 
        this.context = c; 
       } 

       //@Override 
       protected void onPreExecute() 
       { 
        dialog = new ProgressDialog(context); 
       dialog.setMessage("Scanning Ports!"); 
        dialog.show(); 
       } 
       @Override 
       protected Integer doInBackground(String... params) { 
        // TODO Auto-generated method stub 
        for(intBeg = intBeg; intBeg <= intEnd; intBeg++    
        { 
         try 
         { 
         Socket s = new Socket(strMachine, intBeg); 
         isConnected += strMachine + " Is Listening On Port: " + intBeg + "\n"; 
         Log.d("PORTSCAN", isConnected); 
         s.close(); 
         }catch(Exception e){ 
          notConnected += strMachine + " Is Not Listening On Port: " + intBeg + "\n";       
          //Toast.makeText(getBaseContext(), e.getMessage(), 3000).show(); 
          Log.d("PORTSCAN", notConnected); 

         } 
        } 


        return 1; 
       } 

       protected void onPostExecute(Integer... params) 
       { 

       } 
       protected void onProgressUpdate(String... params) 
       { 

       } 





      } 

然而,當doInBackground完成後,它應該調用onPostExecute(),它永遠不會發生。即使當我嘗試在onPostExecute()上使用「@Override」註釋時,它也會給我一個錯誤,說明onPostExecute必須重載超類方法。我沒有得到正在發生的事情!任何幫助?謝謝!

+0

確保您已導入正確的類,它是, android.os.AsyncTask jeet

回答

2

onPostExcecute(Integer result)只有一個參數(no ...)。

如果參數不匹配,它將不匹配super方法,因此不會覆蓋它(並被調用)。通常如果@Override給你一個錯誤,你的方法名/參數有問題(或者超類沒有這樣的方法)。

+0

謝謝,它現在很好用!我正在使用(Integer ... params),當它應該只是(Integer params) – Alejandro

1
protected void onPostExecute(Integer params)//its single Integer object, not array of Integer objects 

它的java Varargsand you can't change parameter of overrided method,你只需要改變倍率的行爲method.And其AsyncTask Class.

0

方法只要使用這個onPostExecuteMethod

@Override 
protected void onPostExecute(Integer result) { 
    super.onPostExecute(result); 
    Log.e("LOG", " on Post"); 
} 
0

檢查這個代碼

private class DownloadingProgressTask extends 
     AsyncTask<String, Void, Boolean> { 

    private ProgressDialog dialog = new ProgressDialog(ShowDescription.this); 

    /** progress dialog to show user that the backup is processing. */ 

    /** application context. */ 

    protected void onPreExecute() { 
     this.dialog.setMessage("Please wait"); 
     this.dialog.show(); 
    } 

    protected Boolean doInBackground(final String... args) { 
     try { 
      downloadFile(b.getString("URL")); 
      return true; 
     } catch (Exception e) { 
      Log.e("tag", "error", e); 
      return false; 
     } 
    } 

    @Override 
    protected void onPostExecute(final Boolean success) { 

     if (dialog.isShowing()) { 
      dialog.dismiss(); 
     } 

     if (success) { 
      Toast.makeText(ShowDescription.this, 
        "File successfully downloaded", Toast.LENGTH_LONG) 
        .show(); 
      imgDownload.setVisibility(8); 
     } else { 
      Toast.makeText(ShowDescription.this, "Error", Toast.LENGTH_LONG) 
        .show(); 
     } 
    } 

} 
相關問題