android - 如何动态更改我的 TextView?
问题描述
所有方法都在 MainActivity 中调用。在我的应用程序中,我从 ion 库中获取了一个 HTML 页面,并且我有一些方法可以格式化我用 ion 下载的文本。
我想要做的是DoveSono
在 TextView 中设置变量,实际上在循环的每个循环中,变量都会改变,所以 TextView 也应该改变,但实际上我所做的应用程序崩溃了。
这是我调用 onClick 并从获取 HTML 网站开始的方法,然后它调用其他方法:
private void getHTMLArticoli(){
// progressDialog = new SpotsDialog(articoli.this, R.style.Custom);
// progressDialog.show();
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(new Runnable() {
@Override
public void run() {
htmlresultart = null;
try {
htmlresultart = Ion.with(getApplicationContext())
.load("WEBSITEIP")
.asString()
.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
if (htmlresultart != null) {
htmlresultart = htmlresultart.replace("</td>", "\n");
getTableArticoli();
getTestataArticoli();
getBodyArticoli();
// progressDialog.cancel();
}
虽然这里是我在 getHTMLArticoli 中调用的 getBodyArticoli 方法,但您可以找到我尝试设置文本的部分
private void getBodyArticoli(){
DaDoveParto = Integer.valueOf(String.valueOf(htmlresultart.indexOf("TBLCRP")));
DoveMiFermo = Integer.valueOf(String.valueOf(htmlresultart.indexOf("</form>")));
if(DaDoveParto == 0){
Toast.makeText(this,"NESSUN DATO TROVATO",Toast.LENGTH_SHORT).show();
}else
{
Integer i;
Integer j;
Integer CONTACAMPO = 0;
for( i = DaDoveParto ; i <= DoveMiFermo ; i++){
if( htmlresultart.substring(i, i + 4).equals("<td>")){
i += 4;
for (j = i; j <= DoveMiFermo ; j++){
if(htmlresultart.substring(j, j + 1).equals("\n")){
appBODYart[CONTACAMPO] = htmlresultart.substring(i, i + (j - i));
if(appBODYart[CONTACAMPO].equals("(null)")){
appBODYart[CONTACAMPO] = "";
}
CONTACAMPO += 1;
if(CONTACAMPO.equals(QuantiCampi)){
CONTACAMPO = 0;
myDB.insertArtServer(appBODYart[0], appBODYart[1], appBODYart[2], appBODYart[3], appBODYart[4], appBODYart[5],
appBODYart[6], appBODYart[7], appBODYart[8]);
DoveSono +=0;
textView.setText(String.valueOf(DoveSono);
}
break;
实际上,当我尝试这样做时,我收到以下错误:
05-31 17:24:41.515 4605-4769/com.example.igardini.visualposmobile E/AndroidRuntime:致命异常:pool-2-thread-1 进程:com.example.igardini.visualposmobile,PID:4605 android.view。 ViewRootImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能接触其视图。在 android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6363) 在 android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:874) 在 android.view.View.requestLayout(View.java:17484) 在 android.view。 View.requestLayout(View.java:17484) 在 android.view.View.requestLayout(View.java:17484) 在 android.view.View.requestLayout(View.java:17484) 在 android.view.View.requestLayout(View .java:17484) 在 android.view.View.requestLayout(View.java:
解决方案
我只需要包装
textView.setText(String.valueOf(DoveSono * 100 / QuanteRighe));
和
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(String.valueOf(DoveSono * 100 / QuanteRighe));
}
});
推荐阅读
- python - 'b' 字符在被新行分割后的第一个位置
- ruby - 我可以使用不在我当前文件夹层次结构下的另一个文件夹中的 Stepdefinitions 吗?
- javascript - Firebase Cloud Functions 会立即删除节点,而不是在 2 小时后删除
- javascript - 递归查找children的children并移除
- c# - 会话参数为空 ASP.NET MVC
- sql-server - 在一天中的特定时间之后 - 哪种方法更好?
- c - 如何使用 i2c 程序和树莓派(C 语言)配置霍尔效应传感器(si7210)
- php - 为什么我自己跟踪代码时得到的输出是 20 而我得到的是 16?
- javascript - 在节点的顶层使用异步等待初始化多个数据库
- android - 预期 BEGIN_ARRAY 但在 gson、android、改造时出现 BEGIN_OBJECT 错误