Android文本视图未更新

Android文本视图未更新,android,multithreading,timer,textview,Android,Multithreading,Timer,Textview,各位。 我想为Android做一个基本的大亨游戏 我试着用定时器每5秒增加一次文本浏览量, 但是文本视图不会更新。 以下是我目前的代码: public class Town extends Activity implements OnClickListener { Timer timer; TimerTask task; TextView goldTV; TextView woodTV; TextView foodTV; TextView stoneTV; TextView cashTV; in

各位。 我想为Android做一个基本的大亨游戏 我试着用定时器每5秒增加一次文本浏览量, 但是文本视图不会更新。 以下是我目前的代码:

public class Town extends Activity implements OnClickListener {
Timer timer;
TimerTask task;
TextView goldTV;
TextView woodTV;
TextView foodTV;
TextView stoneTV;
TextView cashTV;
int gold = 20;
int wood = 20;
int food = 20;
int stone = 20;
int cash = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_town);
    goldTV = (TextView) findViewById(R.id.textView1);
    woodTV = (TextView) findViewById(R.id.TextView01);
    foodTV = (TextView) findViewById(R.id.TextView02);
    stoneTV = (TextView) findViewById(R.id.TextView03);
    cashTV = (TextView) findViewById(R.id.TextView04);
    timer = new Timer();
    task = new TimerTask() {

        @Override
        public void run() {
            gold++;
            goldTV.setText(gold);
            try {
                this.wait(2000);
            }
            catch (InterruptedException e){
            }
        }

    };
}
@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

}

}您的问题是您正在更改UI线程以外的其他内容上的文本。最好是在UI线程上运行它。另外,你应该把你的号码转换成字符串,否则Android会认为你在寻找一个资源id。把它们放在一起,然后

task = new TimerTask() {

    @Override
    public void run() {
        gold++;
        runOnUiThread(new Runnable(){
           public void run(){
            goldTV.setText(""+gold);
           }
        });
        try {
            this.wait(5000);
        }
        catch (InterruptedException e){
        }
    }

};
或者更好的是,您可以使用处理程序,如下所示:

Handler handler = new Handler();
Runnable task=new Runnable(){
   public void run(){
      handler.postDelayed(this,5000);
      goldTV.setText(""+gold);
   }
});
handler.postDelayed(task,5000);

您的问题是您正在更改UI线程以外的其他内容上的文本。最好是在UI线程上运行它。另外,你应该把你的号码转换成字符串,否则Android会认为你在寻找一个资源id。把它们放在一起,然后

task = new TimerTask() {

    @Override
    public void run() {
        gold++;
        runOnUiThread(new Runnable(){
           public void run(){
            goldTV.setText(""+gold);
           }
        });
        try {
            this.wait(5000);
        }
        catch (InterruptedException e){
        }
    }

};
或者更好的是,您可以使用处理程序,如下所示:

Handler handler = new Handler();
Runnable task=new Runnable(){
   public void run(){
      handler.postDelayed(this,5000);
      goldTV.setText(""+gold);
   }
});
handler.postDelayed(task,5000);

TimerTask应与Timer对象一起使用。在代码中,您从未运行过任务

编辑: 请尝试以下方法:

goldTV.postDelayed(new Runnable() {

            @Override
            public void run() {
                gold++;
                goldTV.setText(gold+"");
                goldTV.postDelayed(this,2000);
            }
        }, 2000);

TimerTask应与Timer对象一起使用。在代码中,您从未运行过任务

编辑: 请尝试以下方法:

goldTV.postDelayed(new Runnable() {

            @Override
            public void run() {
                gold++;
                goldTV.setText(gold+"");
                goldTV.postDelayed(this,2000);
            }
        }, 2000);
在你的跑步方法中

您调用的是setTextint resId,而不是setTextCharSequence c

要显示实际的整数gold,请将其从int转换为String

在你的跑步方法中

您调用的是setTextint resId,而不是setTextCharSequence c

要显示实际的整数gold,请将其从int转换为String