gpt4 book ai didi

java - CallBack 给出 NullPointEreException

转载 作者:行者123 更新时间:2023-12-02 07:46:27 26 4
gpt4 key购买 nike

我正在尝试让一个简单的网络阅读器在 AsyncTast 中工作,

主线程

public class main extends Activity implements CallBackListener{
...
WebReader wr = new WebReader();
wr.execute("http://www.google.com");
...
@Override
public void callback(String res)
{
Log.e("eee",res);
}
...
}

...
interface CallBackListener
{
void callback(String res);
}

和网络阅读器

class WebReader extends AsyncTask<String, Void, String>{


CallBackListener mListener;

public void setListener(CallBackListener listener){
mListener = listener;
}


@Override
protected String doInBackground(String... urls) {

String response = "";

DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(urls[0]);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();

BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}

} catch (Exception e) {
e.printStackTrace();
}

return response;
}


@Override
protected void onPostExecute(String result) {
Log.e("s3","ss"+result);
mListener.callback(result); // null point here
}



}

但是它在 mListener.callback 处因空点而崩溃......你知道可能出了什么问题吗?

谢谢!

最佳答案

您没有设置监听器。你应该有这样的东西:

 WebReader wr = new WebReader();
wr.setListener( new YourCallbackListener() );
wr.execute("http://www.google.com");

关于java - CallBack 给出 NullPointEreException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10773715/

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