【问题标题】:Only the original thread that created a view hierarchy can touch its views?只有创建视图层次结构的原始线程才能触及它的视图?
【发布时间】:2018-04-12 23:25:27
【问题描述】:

我无法解决此错误: android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能接触其视图。

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

我无法更改第 45 行中的 score. score.setText(scorenewe.toString());

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)

【问题讨论】:

    标签: java android timer


    【解决方案1】:

    这是因为您试图在不在 UI 线程中时触摸视图。

    快速修复如下所示:

    之前的代码

    score.setText(scorenewe.toString());
    

    之后的代码:

    new Handler(Looper.getMainLooper()).post(new Runnable(){
        @Override
        public void run() {
            score.setText(scorenewe.toString());
        }
    });
    

    这样你会告诉 Android 框架在主 UI 线程中运行这行代码,在那里你可以触摸任何你想要的视图 P.S. read this

    【讨论】:

      【解决方案2】:

      解决方案:

      new Timer().scheduleAtFixedRate(new TimerTask() {
              public void run() {
                      RunOnUiThread(new Runnable() {
            @Override
            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);
      

      function RunOnUIThread 是一个 Activity 函数。

      【讨论】:

      • 我收到消息“无法解析方法'RunOnUiThread(anonymous java.lang.Runnable)'”
      • 由于您的代码未显示其 Fragment 或 Activity..i 是否为 Activity..i 发布,因此 LionisIAm 的代码将适用于 Fragment..
      猜你喜欢
      • 2011-11-20
      • 1970-01-01
      • 2011-07-06
      • 2011-03-17
      • 2012-12-26
      • 1970-01-01
      相关资源
      最近更新 更多