2 annbnncnnd AnnBnnCnnD 于 2013.01.18 11:32 提问

在异步任务中无法调用view

在Asynctask类中调用另一个类的view,但是没成功,请帮忙。

异步任务:

private class parseSite extends AsyncTask<String, Void, List<Integer>> {

    protected List<Integer> doInBackground(String... arg) {
     List<Integer> output = new ArrayList<Integer>();
        try {
            htmlHelper hh = new htmlHelper(new URL(arg[0]));
            output = hh.htmlHelper(arg[0]);
        } catch (Exception e) {
            System.out.println("Error");
        }
        return output;
    }

    protected void onPostExecute(List<Integer> exe) {

        graph salesView = new graph();
        View chartView = salesView.getView(this);
        chartView.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT, 1f));
        LinearLayout layout = (LinearLayout) findViewById(R.id.linearview);

        layout.addView(chartView, 0);

    }
}

activity的graph:

public class graph {

public View getView(Context context) (...etc.)

谢谢:-D

2个回答

cytown
cytown   2013.01.18 14:16

设置一个handler,当执行需要更改ui的时候,通过handler.sentMessage去通知handle里面去更改UI.

zxl881209
zxl881209   2014.07.29 00:05

异步方法种怎么能更新UI呢,设置一个Handler,
在onPostExecute()方法种发送一个handler.sentMessage();通知handler更新UI;

Csdn user default icon
上传中...
上传图片
插入图片