Android错误异常:E / AndroidRuntime(15779):android.view.ViewRootImpl $ CalledFromWrongThreadException:

时间:2016-01-21 15:27:21

标签: java android multithreading

我在运行游戏时不断收到此错误消息:

01-21 16:14:00.911: E/AndroidRuntime(15779): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-21 16:14:00.911: E/AndroidRuntime(15779):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6373)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:878)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at android.view.View.requestLayout(View.java:17566)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at android.view.View.requestLayout(View.java:17566)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at android.view.View.requestLayout(View.java:17566)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at android.view.View.requestLayout(View.java:17566)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:361)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at android.view.View.requestLayout(View.java:17566)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at android.widget.ScrollView.requestLayout(ScrollView.java:1483)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at android.view.View.requestLayout(View.java:17566)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:361)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at android.view.View.requestLayout(View.java:17566)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at android.widget.TextView.checkForRelayout(TextView.java:6914)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at android.widget.TextView.setText(TextView.java:4096)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at android.widget.TextView.setText(TextView.java:3954)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at android.widget.TextView.setText(TextView.java:3929)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at com.gamerscave.corpboss.Game.Overview_Viewupdate(Game.java:305)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at com.gamerscave.corpboss.Game.tick(Game.java:174)
01-21 16:14:00.911: E/AndroidRuntime(15779):    at com.gamerscave.corpboss.game.GameThread.run(GameThread.java:27)

此错误代码由以下原因引起:

    public void Overview_Viewupdate(){
    tv1.setText("Balance: " + Maths.coustomFormat(DEFAULT_BIG_NUMBER_PATTERN, bal));
    tv2.setText("Income: " + Maths.coustomFormat(DEFAULT_BIG_NUMBER_PATTERN, inc));
    tv3.setText("Nett worth: " + Maths.coustomFormat(DEFAULT_BIG_NUMBER_PATTERN, NettWorth));
    tv4.setText("Shares: " + sharePercent + "%");
    tv5.setText("Share value: " + ShareVal);
    tv6.setText("Date: " + Date);
}

并在:

调用
    public void tick(){
    player.tick();

    long elapsed = (System.nanoTime()-startTime)/1000000;

    if(elapsed>1000)
    {
        nextDay(day, year, month);
        System.out.println("New day: " + day);
        startTime = System.nanoTime();
    }

    if(view1){
        Overview_Viewupdate();
    }

}

由于某种原因,它不会在发布时触发。在init方法中调用启动更改。从GameThread类调用init方法'运行方法。那么我该如何避免这个错误并且还有一个有效的游戏线程呢?

2 个答案:

答案 0 :(得分:1)

您的问题是您正在更新非UI线程上的UI元素。有几种方法可以解决这个问题。在您的情况下,我认为最好的方法是使用RunOnUiThread语句,如下所示(注意,这必须在活动上完成):

RuOnUiThread(new Runnable(){ void run(){     Overview_Viewupdate(); } }); 您的问题是您正在更新非UI线程上的UI元素。有几种方法可以解决这个问题。在您的情况下,我认为最好的方法是使用RunOnUiThread语句,如下所示(注意,这必须在活动上完成):

RuOnUiThread(new Runnable() {
void run() {
    Overview_Viewupdate();
}
});

此代码将替换您当前的Overview_ViewUpdate调用。或者,您可以考虑AysncTask,其中OnPostExecute运行Overview_ViewUpdate(),而您的doInBackground()执行必须在后台运行的操作。

此外,请确保在UI线程上创建UI元素。这通常在OnCreateOnStart中完成。 AddContentView(...)SetContentView(...)计算在内。

答案 1 :(得分:1)

上述错误的发生是因为Android不希望其他线程(如计时器)弄乱其GUI。所以,你需要做的是礼貌地要求Android为你做更新。

public void tick(){
  player.tick();

  long elapsed = (System.nanoTime()-startTime)/1000000;

  if(elapsed>1000)
  {
    nextDay(day, year, month);
    System.out.println("New day: " + day);
    startTime = System.nanoTime();
  }

  if(view1){
      runOnUiThread(new Runnable() {
        public void run(){  
           Overview_Viewupdate(); 
      }   
  });
 }

}