【问题标题】:Android AsyncTask errors with onPostExecute带有 onPostExecute 的 Android AsyncTask 错误
【发布时间】:2012-06-15 08:57:18
【问题描述】:

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

【问题讨论】:

  • 确保您已导入正确的类,即 android.os.AsyncTask

标签: java android android-asynctask


【解决方案1】:

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

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

【讨论】:

  • 谢谢,现在很好用!我正在使用(整数...参数),而它应该只是(整数参数)
【解决方案2】:
 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,你只需改变覆盖方法的行为。以及它的AsyncTask Class.方法

【讨论】:

    【解决方案3】:

    只需使用这个 onPostExecuteMethod

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

    【讨论】:

      【解决方案4】:

      检查此代码

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-17
        • 2017-07-23
        • 1970-01-01
        • 1970-01-01
        • 2015-03-31
        • 1970-01-01
        • 2023-03-25
        相关资源
        最近更新 更多