没有调用AsyncTask onPreExecute

时间:2011-07-30 12:21:41

标签: android android-asynctask

由于某种原因,没有调用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

Screenshot

1 个答案:

答案 0 :(得分:4)

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

您正在调用它onPreExcecute(注意'x'之后的'c'不应该在那里)。将其更正为onPreExecute并使用@Override