简体   繁体   English

Android AsyncTask错误与onPostExecute

[英]Android AsyncTask errors with onPostExecute

i have a question regarding the AsyncTask class in android, and why it is giving me an error. 我有一个关于android中的AsyncTask类的问题,为什么它给我一个错误。 I have defined an inner class here.. 我在这里定义了一个内部类。

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)
                {

                }





            }

However, when the doInBackground finishes, it supposed to call onPostExecute(), which never happens. 但是,当doInBackground完成时,它应该调用onPostExecute(),但从未发生。 And even when i try to use the "@Override" annotation over the onPostExecute(), it gives me an error saying onPostExecute must override a superclass method. 即使我尝试在onPostExecute()上使用“ @Override”批注,也给我一个错误,指出onPostExecute必须重写超类方法。 I dont get what is happening! 我不知道发生了什么! any help? 有什么帮助吗? Thanks! 谢谢!

onPostExcecute(Integer result) takes a single argument (no ...). onPostExcecute(Integer result)接受一个参数(否...)。

If the arguments don't match, it will not match the super method and will therefore not override it (and be called). 如果参数不匹配,则它将不匹配super方法,因此将不会覆盖它(并被调用)。 Generally if @Override gives you an error something is wrong with your method name/parameters (or the superclass does not have such a method). 通常,如果@Override给您一个错误,则您的方法名称/参数有问题(或超类没有这样的方法)。

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

its java Varargs . 其Java Varargs and you can't change parameter of overrided method ,You just change the Behaviour of override method.And its method of AsyncTask Class. and you can't change parameter of overrided method ,只需更改and you can't change parameter of overrided method的行为。及其AsyncTask Class.方法AsyncTask Class.

Just use this onPostExecuteMethod 只需使用此onPostExecuteMethod

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

Check this code 检查此代码

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