繁体   English   中英

Android AsyncTask错误与onPostExecute

[英]Android AsyncTask errors with onPostExecute

我有一个关于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必须重写超类方法。 我不知道发生了什么! 有什么帮助吗? 谢谢!

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

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

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

其Java Varargs and you can't change parameter of overrided method ,只需更改and you can't change parameter of overrided method的行为。及其AsyncTask Class.方法AsyncTask Class.

只需使用此onPostExecuteMethod

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

检查此代码

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();
        }
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM