使用onPostExecute时出现Android AsyncTask错误

时间:2012-06-12 13:04:49

标签: java android android-asynctask

我有一个关于android中的AsyncTask类的问题,以及为什么它给我一个错误。我在这里定义了一个内部类..

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必须覆盖超类方法。我不知道发生了什么!任何帮助?谢谢!

4 个答案:

答案 0 :(得分:2)

onPostExcecute(Integer result)只接受一个参数(不...)。

如果参数不匹配,则它将与super方法不匹配,因此不会覆盖它(并被调用)。通常,如果@Override给出错误,那么您的方法名称/参数会出错(或者超类没有这样的方法)。

答案 1 :(得分: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,您只需更改覆盖方法的行为。及其方法AsyncTask Class.

答案 2 :(得分:0)

只需使用此onPostExecuteMethod

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

答案 3 :(得分: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();
        }
    }

}