2016-07-31 72 views
0

我是android編程新手。我已經創建了一個應用程序,可以填充數據並在表格中顯示此數據。但是,我得到一個錯誤:錯誤:「必須從UI線程調用方法getText(),當前推斷的線程是worker。」

Method getText() must be called from the UI Thread, currently inferred thread is worker

下面幾行

weight.getText() txtDate.getText() txtTime.getText()

在此代碼:

@Override 
    protected Void doInBackground(Void... params) { 

     String w_value = weight.getText().toString(); 
     String wdate = txtDate.getText().toString(); 
     String wtime = txtTime.getText().toString(); 

     // inserting data 
     sqlcon.open(); 
     sqlcon.insertData(w_value, wdate, wtime); 
     BuildTable(); 
     return null; 
    } 

重量 - 顯示數(27.6,5,0)

txtDate - 顯示日期(20-09-2016,12-03-1988)

txtTime - 顯示時間(15:16,1:06)

我insertData方法:

public void insertData(String weight, String date) { 

    ContentValues cv = new ContentValues(); 
    cv.put(MyDbHelper.WEIGHT, weight); 
    cv.put(MyDbHelper.WEIGHT_DATE, String.valueOf(date)); 
    cv.put(MyDbHelper.WEIGHT_TIME, new SimpleDateFormat("HH:mm").format(new Date())); 
    database.insert(MyDbHelper.TABLE_WEIGHT, null, cv); 

} 

回答

1

的錯誤是很清楚,我想。您必須從UI線程中調用getText,但不是 - 您是從工作線程中的doInBackground()調用它的。這是不允許的。只有UI線程才能訪問UI元素。

相關問題