0

我有一个关于 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

4 回答 4

2

onPostExcecute(Integer result)接受一个参数(没有...)。

如果参数不匹配,它将不匹配超级方法,因此不会覆盖它(并被调用)。一般来说,如果@Override给你一个错误,你的方法名称/参数有问题(或者超类没有这样的方法)。

于 2012-06-12T13:08:49.933 回答
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.

于 2012-06-12T13:10:42.517 回答
0

只需使用这个 onPostExecuteMethod

@Override
protected void onPostExecute(Integer result) {
    super.onPostExecute(result);
    Log.e("LOG", " on Post");
}
于 2012-06-12T13:16:08.023 回答
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();
        }
    }

}
于 2012-06-12T13:17:34.980 回答