0

我正在尝试让一个简单的网络阅读器在 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 处因空点而崩溃......任何想法可能有什么问题?

谢谢!

4

1 回答 1

2

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

 WebReader wr = new WebReader();
 wr.setListener( new YourCallbackListener() );
 wr.execute("http://www.google.com");
于 2012-05-27T12:14:04.940 回答