【问题标题】:The method execute() is undefined for the type InvoiceTemplateGet未为 InvoiceTemplateGet 类型定义方法 execute()
【发布时间】:2015-04-22 19:57:38
【问题描述】:

我使用 AsyncTask 从 API 获取结果。我有几个课程很好。我创建了另一个 AsyncTask 类,但出现此错误:

The method execute() is undefined for the type InvoiceTemplateGet

在这一行:

InvoiceTemplateGet template = new InvoiceTemplateGet(PaymentMethodActivity.this, invoiceNumber);
template.execute();

有类InvoiceTemplateGet:

public class InvoiceTemplateGet {

    public InvoiceTemplateGet(Context context, String invoiceNumber){
        mContext = context;
        this.invoiceNumber = invoiceNumber;
        load_default_settings();
    }


    protected CustomAsyncTaskResult<String> doInBackground(String... arg0) {
        try{
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(url);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String response = httpclient.execute(httpget,responseHandler);
            return new CustomAsyncTaskResult<String>(response);
        }catch (HttpResponseException e){
            return new CustomAsyncTaskResult<String>(new ApiException("Invoice", "Invoice was not found."));
        }catch (Exception e){
            Log.e("Invoice Error", "Error:", e);
            return new CustomAsyncTaskResult<String>(new ApiException("Invoice", e.getMessage()));
        }
    }

    protected void onPostExecute(CustomAsyncTaskResult<String> result) {
        ((PaymentMethodActivity)mContext).getResultFromTemplate(result);
        progressDialog.dismiss();
    }
}

我有另一个和这个一样的类,只是 url 不同。它像这个页面一样在同一页面上执行,在同一部分代码上。它有效,为什么我仍然收到此错误?

【问题讨论】:

  • 可能其他类是直接扩展AsyncTask
  • 天哪...你能把它写成答案吗?
  • 看起来您从某处复制粘贴了 InvoiceTemplateGet 类的主体,但您忘记将 extends AsyncTask&lt;String, Void, CustomAsyncTaskResult&lt;String&gt;&gt; 添加到类声明中

标签: android android-asynctask execution


【解决方案1】:

我有另一个和这个一样的类,只是 url 是 不同的。它像这个一样在同一个页面上执行,在同一个 代码的一部分。它有效,为什么我仍然收到此错误?

您的InvoiceTemplateGet 没有名为execute() 的方法。查看您的代码,在我看来您忘记了extend AsyncTask。您还可以创建您的 execute() 方法,该方法实例化内部 AsyncTask 并调用 instance.execute()

【讨论】:

    猜你喜欢
    • 2022-11-03
    • 2014-06-15
    • 2017-04-23
    • 1970-01-01
    • 2022-12-23
    • 2021-08-02
    • 2017-12-21
    • 2013-12-16
    • 2016-10-14
    相关资源
    最近更新 更多