Android TimerTask崩溃程序致命异常时间-0

时间:2012-01-03 07:22:33

标签: android timer timertask

所以我环顾四周,看了几篇关于类似事情的帖子,但它们都与地理位置有关,我想做的就是一个简单的计时器。基本上我使用TimerTask作为活动类中30秒的倒计时。每次调用TimerTask时应用程序都会崩溃。出于上下文的目的,这是来自父级的子活动,它使用startActivityForResult调用来调用它。

public class NewLevelActivity extends Activity {

    int time = 30;
    int level = 1;
    TextView text;
    TimerTask task;


   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.new_level);
      text = (TextView) findViewById(R.id.newLevelText);
      level = WholettheballoutActivity.level;


      task = new TimerTask() {
        public void run() {
                    time--;
                    //text.setText("Level " + Integer.toString(level)+ " will start in " +Integer.toString(time) + " seceonds." );
                    new Timer().schedule(task,1000);
               }
        };

        new Timer().schedule(task,1000);

   }


   @Override
   protected void onPause() {
       super.onPause();

   }

   @Override
   protected void onResume() {
       super.onResume();
    }




}

logcat显示了这个

01-03 01:08:57.630: ERROR/AndroidRuntime(19618): FATAL EXCEPTION: Timer-0
01-03 01:08:57.630: ERROR/AndroidRuntime(19618): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-03 01:08:57.630: ERROR/AndroidRuntime(19618):     at android.view.ViewRoot.checkThread(ViewRoot.java:3441)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618):     at android.view.ViewRoot.requestLayout(ViewRoot.java:586)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618):     at android.view.View.requestLayout(View.java:10781)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618):     at android.view.View.requestLayout(View.java:10781)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618):     at android.view.View.requestLayout(View.java:10781)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618):     at android.view.View.requestLayout(View.java:10781)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618):     at android.view.View.requestLayout(View.java:10781)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618):     at android.widget.TextView.checkForRelayout(TextView.java:6052)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618):     at android.widget.TextView.setText(TextView.java:2826)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618):     at android.widget.TextView.setText(TextView.java:2691)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618):     at android.widget.TextView.setText(TextView.java:2666)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618):     at org.kizik.WLTBO.NewLevelActivity$1.run(NewLevelActivity.java:43)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618):     at java.util.Timer$TimerImpl.run(Timer.java:284)

我试图设置视图的文本时遇到了问题,但事实并非如此,因为在注释掉设置文本部分之后它仍然是错误的

3 个答案:

答案 0 :(得分:2)

您正在尝试从非UI线程更新您的UI,因此您需要将您的内容更新为runOnUiThread()

中的UI
Activity_name.this.runOnUiThread(new Runnable() {
    public void run() {
      text.setText("Level " + Integer.toString(level)+ " will start in " +Integer.toString(time) + " seceonds." );
    }
});

答案 1 :(得分:0)

public class NewLevelActivity extends Activity {

int time = 30;
int level = 1;
TextView text;
TimerTask task;




@Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.new_level);
      text = (TextView) findViewById(R.id.newLevelText);
      level = WholettheballoutActivity.level;


      task = new TimerTask() {
        public void run() {
                    time--;
                    textChangeHandler.sendEmptyMessage(0);
                    new Timer().schedule(task,1000);
               }
        };

        new Timer().schedule(task,1000);

   }


   @Override
   protected void onPause() {
       super.onPause();

   }

   @Override
   protected void onResume() {
       super.onResume();
    }

    private Handler textChangeHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
                  text.setText("Level " + Integer.toString(level)+ " will start in " +Integer.toString(time) + " seceonds." );
        }
    };


}

你无法从线程修改ui ..你可以借助处理程序

来完成

答案 2 :(得分:0)

当你的任务完成时,

yourTaskObj.cancel()。例如。 task.cancel();