gpt4 book ai didi

android - AsyncTask onPreExecute 未被调用

转载 作者:搜寻专家 更新时间:2023-11-01 08:13:51 26 4
gpt4 key购买 nike

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

最佳答案

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

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

关于android - AsyncTask onPreExecute 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6883068/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com