gpt4 book ai didi

java - 错误: “Method getText() must be called from the UI thread, currently inferred thread is worker.”

转载 作者:行者123 更新时间:2023-12-01 09:37:59 24 4
gpt4 key购买 nike

我是 Android 编程新手。我制作了一个应用程序,我可以在其中填充数据,并且应用程序在表格中显示这些数据。但我收到一个错误:

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);

}

最佳答案

我认为这个错误非常明显。您必须从 UI 线程调用 getText,但实际上不是——您是从工作线程中的 doInBackground() 调用它。这是不允许的。只有 UI 线程可以访问 UI 元素。

关于java - 错误: “Method getText() must be called from the UI thread, currently inferred thread is worker.” ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38688735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com