作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法解决这个错误:android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建 View 层次结构的原始线程才能触摸其 View 。
TextView score;
private SharedPreferences speicher;
private SharedPreferences.Editor editor;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
score = (TextView) findViewById(R.id.score);
speicher = getApplicationContext().getSharedPreferences("Daten", 0);
editor = speicher.edit();
loadfile("score" , score);
new Timer().scheduleAtFixedRate(new TimerTask() {
public void run() {
Integer scorealt = Integer.parseInt(speicher.getString("score", null));
Integer scorenewe = scorealt + Integer.parseInt(speicher.getString("anz", null));
score.setText(scorenewe.toString());
savefile("score", scorenewe.toString());
}
}, 0, 2000);
}
而且我无法更改分数。 Score.setText(scorenewe.toString());
第 45 行
android.view.ViewRootImpl$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7769)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1332)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:5446)
at android.view.View.invalidateInternal(View.java:14750)
at android.view.View.invalidate(View.java:14714)
at android.view.View.invalidate(View.java:14698)
at android.widget.TextView.checkForRelayout(TextView.java:8535)
at android.widget.TextView.setText(TextView.java:5076)
at android.widget.TextView.setText(TextView.java:4901)
at android.widget.TextView.setText(TextView.java:4876)
at de.yt.tutorial.Home$1.run(Home.java:45)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
最佳答案
这是因为您尝试在不在 UI 线程中时触摸 View 。
快速修复如下所示:
之前的代码
score.setText(scorenewe.toString());
之后的代码:
new Handler(Looper.getMainLooper()).post(new Runnable(){
@Override
public void run() {
score.setText(scorenewe.toString());
}
});
这样你就可以告诉 Android 框架在主 UI 线程中运行这行代码,你可以在其中触摸你想要的任何 View 。 read this
关于java - 只有创建 View 层次结构的原始线程才能触摸它的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47041396/
我正在尝试将多个水平链接的 Button 和 TextView 垂直链接为 View 集,但仍保持平面 View 层次结构。这是我的初始布局和代码:
到目前为止,我已经在Google BigQuery上训练了几种模型,目前我需要查看模型的外观(即架构,损失函数等)。 有没有办法获取这些信息? 最佳答案 仔细阅读文档后,我可以说该功能尚不存在。我什至
本文实例讲述了PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)。分享给大家供大家参考,具体如下: 前言: 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个
我是一名优秀的程序员,十分优秀!