0

任何一个更聪明、眼睛更好的人能解决这个问题吗?执行时会抛出这样的消息: 原因:“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);


    }
}

}

4

2 回答 2

0

I dont know what are you trying to show.. but that error is when you try to put something on the UI thread like a dialog from a different thread. Try using handle or runonuithread.

于 2013-09-03T10:51:45.323 回答
0

你应该使用这个:

runOnUiThread(new Runnable(){
    public void run() {
//Code where you handle any view (where you get an error)
}
};
于 2013-09-03T11:04:19.240 回答