Async onPostExecute未被调用或正确覆盖

时间:2014-10-25 14:48:55

标签: java android asynchronous override

我对Android开发很新,但推断我没有正确覆盖onPostExecute方法(错误),但是我错了我错的地方。我是否正确地认为onPostExecute是由主UI线程而不是Async调用的?目前根本没有调用onPostExecute,添加@Override给了我错误..

基本上,我只是下载图片。学习这个异步业务和Java。

class ImageReceiver extends AsyncTask<String, Integer, Bitmap>{

private View rootView;

public ImageReceiver(View rootView){
    this.rootView=rootView;
}

@Override
protected Bitmap doInBackground(String... params) {
    System.out.println("got here");
    try {

        URL url = new URL(params[0]);
        HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();

        if(httpCon.getResponseCode() !=200){
            throw new Exception("Failed to connect");
        }


    InputStream is = httpCon.getInputStream();
    Bitmap img = BitmapFactory.decodeStream(is);
    System.out.println(url.toString());

    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
@Override
protected void OnPostExecute(Bitmap img){
    System.out.println("postexecute");

    ImageView im = (ImageView)rootView.findViewById(R.id.incomming_image);
    im.setImageBitmap(img);
}
}

我在MainActivity的OnCreate方法中调用new thead就像这样。

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

    View = getWindow().getDecorView().findViewById(android.R.id.content);



    ImageReceiver imageReceiver = new ImageReceiver(View);
    imageReceiver.execute("http://joshuacroft.co.uk/images/killzone_mercenary_2.jpg");

}

不确定我可以纠正的地方,所以感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

您遇到区分大小写的错误。见字母o

OnPostExecute(Bitmap img)更改为onPostExecute(Bitmap img)

也不要忘记致电super.onPostExecute(bitmap);。对于每个重写方法,最好将super方法作为第一个语句调用:)

答案 1 :(得分:0)

应为onPostExecute(..) not OnPostExecute(..)