AsyncTask onPreExecute 未被调用

发布于 2024-11-27 14:09:56 字数 2162 浏览 0 评论 0原文

由于某种原因,onPreExecute 没有被调用。代码:

protected void onPreExcecute() {
    hook.createDialog(ticker);
}

整个类:

package com.evandarwin.finance;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
public class GetTicker extends AsyncTask<String, Integer, String>{
    private Context ctx;
    private String ticker;
    private SimpleFinanceActivity hook;

    public GetTicker(Context ctx, String ticker, SimpleFinanceActivity hook) {
        this.ctx = ctx;
        this.ticker = ticker.toUpperCase();
        this.hook = hook;
    }

    protected void onPreExcecute() {
        hook.createDialog(ticker);
    }

    @SuppressWarnings("unused")
    @Override
    protected String doInBackground(String... params) {
        try {
            URL url = new URL("http://finance.yahoo.com/d/quotes.csv?s="+ticker+"&f=a");

            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);

            urlConnection.connect();
            InputStream is = urlConnection.getInputStream();
            StringBuffer str = new StringBuffer();

            int bufferLength = 0; //used to store a temporary size of the buffer

            byte[] stream = new byte[1024];

            while ( (bufferLength = is.read(stream)) > 0 ) {
                str.append(stream);
            }

            return str.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String result) {
        hook.destroyDialog();
        Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
    }
}

这给了我一个 NullPointerException,我知道我以前遇到过这个问题,但我不记得我做了什么来解决它。请帮忙! :P

屏幕截图

For some reason the onPreExecute isn't being called. Code:

protected void onPreExcecute() {
    hook.createDialog(ticker);
}

Entire class:

package com.evandarwin.finance;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
public class GetTicker extends AsyncTask<String, Integer, String>{
    private Context ctx;
    private String ticker;
    private SimpleFinanceActivity hook;

    public GetTicker(Context ctx, String ticker, SimpleFinanceActivity hook) {
        this.ctx = ctx;
        this.ticker = ticker.toUpperCase();
        this.hook = hook;
    }

    protected void onPreExcecute() {
        hook.createDialog(ticker);
    }

    @SuppressWarnings("unused")
    @Override
    protected String doInBackground(String... params) {
        try {
            URL url = new URL("http://finance.yahoo.com/d/quotes.csv?s="+ticker+"&f=a");

            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);

            urlConnection.connect();
            InputStream is = urlConnection.getInputStream();
            StringBuffer str = new StringBuffer();

            int bufferLength = 0; //used to store a temporary size of the buffer

            byte[] stream = new byte[1024];

            while ( (bufferLength = is.read(stream)) > 0 ) {
                str.append(stream);
            }

            return str.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String result) {
        hook.destroyDialog();
        Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
    }
}

This is giving me a NullPointerException, I know I've had this problem before but I don't remember what I did to fix it. Please help! :P

Screenshot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱自由 2024-12-04 14:09:56

@Override 在 Eclipse 中导致问题(以及该方法未被调用的原因)的原因是您输入了错误。

您将其称为onPreExcecute(请注意“x”后面的“c”不应该出现)。将其更正为 onPreExecute 并使用 @Override 来实现。

The reason the @Override is causing a problem in Eclipse (and the reason the method isn't being called) is that you have made a typing error.

You are calling it onPreExcecute (note the 'c' after the 'x' shouldn't be there). Correct it to be onPreExecute and use @Override for that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文