Android异步任务

时间:2013-09-03 10:44:32

标签: android android-asynctask

任何一个更聪明,眼睛更好的人都能解决问题吗? 执行它时会抛出这样的消息:引起:“android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。” 我无法真正克服这个问题...... 请....

public class HourReportsAtv extends Activity implements PropertyChangeListener {
private int weekNum = -1;
private Button addBtn;
private Button prevBtn;
private Button nextBtn;
private ListView workingTimeList;
private TextView weekTotalHour;
private ListViewAdapter adapter;
private int resourceId = R.layout.custom_list_item;
private HourReportCatalog hourReportCatalog = new HourReportCatalog();
ProgressDialog progressDialog;

public HourReportsAtv() {

}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e("test", "");
    setContentView(R.layout.hour_report_atv);
    hourReportCatalog.addChangeListener(this);
    findAllByID();
    // hourReportCatalog =
    // (HourReportCatalog)getIntent().getSerializableExtra("dk.tcmobile.model.HourReportCatalog");
    InitializatorHourReportList task = new InitializatorHourReportList();
    task.execute();  

}
private void findAllByID() {

    addBtn = (Button) findViewById(R.id.AddWeekBtn);
    addBtn.setOnClickListener(new AddBtnListener());
    nextBtn = (Button) findViewById(R.id.NextWeekBtn);
    prevBtn = (Button) findViewById(R.id.PreviousWeekBtn);
    weekTotalHour = (TextView) findViewById(R.id.textView_HourNumber);

    prevBtn.setOnClickListener(new PreviousBtnListener());
    nextBtn.setOnClickListener(new NextBtnListener());
    workingTimeList = (ListView) findViewById(R.id.workingDaysList);

}
public void propertyChange(PropertyChangeEvent evt) {
    String propertyName = evt.getPropertyName();
    if (propertyName.equalsIgnoreCase("total")) {
        Double total = (Double) evt.getNewValue();
        String totalString = Double.toString(total);
        weekTotalHour.setText(totalString);
    }
}    


private class AddBtnListener implements OnClickListener {

    @Override
    public void onClick(View v) {
        hourReportCatalog.createNewHourReport();
    }

}

private class NextBtnListener implements OnClickListener {

    @Override
    public void onClick(View v) {
        weekNum = weekNum + 1;  
        initializeListViewAdapter(hourReportCatalog
                .getOneWeekHourReportList(weekNum));

        double t = hourReportCatalog.getTotal();    
        String totalString = Double.toString(t);
        weekTotalHour.setText(totalString);

    }

}

private class PreviousBtnListener implements OnClickListener {

    @Override
    public void onClick(View v) {
        weekNum = weekNum - 1;
        initializeListViewAdapter(hourReportCatalog
                .getOneWeekHourReportList(weekNum));

        double t = hourReportCatalog.getTotal();
        String totalString = Double.toString(t);
        weekTotalHour.setText(totalString);
    }

}

private boolean isEqual(ArrayList<HourReport> actualList,
        ArrayList<HourReport> oneWeekList) {
    boolean equal = true;

    for (int x = 0; x < actualList.size(); x++) {
        HourReport ahr = actualList.get(x);
        HourReport r = oneWeekList.get(x);
        if (!ahr.equals(r)) {
            equal = false;
            break;
        }
    }
    if (!equal) {

        return false;
    }
    return true;
}

private void initializeListViewAdapter(ArrayList<HourReport> oneWeekList) {
    adapter = new ListViewAdapter(HourReportsAtv.this, resourceId,
            oneWeekList, hourReportCatalog);
    adapter.notifyDataSetChanged();
    //workingTimeList.setAdapter(adapter);

}

private class InitializatorHourReportList extends
        AsyncTask<Void, Void, ArrayList<HourReport>> {

    @Override
    protected ArrayList<HourReport> doInBackground(Void... unused) {
        Log.e("", "doinBackgrounf");
        ArrayList<HourReport> list = new ArrayList<HourReport>();
        list = hourReportCatalog.getOneWeekHourReportList(weekNum);
        return list;
    }

    @Override
    protected void onPostExecute(final ArrayList<HourReport> list) {
        initializeListViewAdapter(list);
        double t = hourReportCatalog.getTotal();    
        String totalString = Double.toString(t);
        weekTotalHour.setText(totalString);


    }
}

}

2 个答案:

答案 0 :(得分:0)

我不知道你想要展示什么......但是当你尝试在UI线程上放置一些东西时,就像来自不同线程的对话框一样。尝试使用handle或runonuithread。

答案 1 :(得分:0)

你应该用这个:

runOnUiThread(new Runnable(){
    public void run() {
//Code where you handle any view (where you get an error)
}
};