Reputation: 179
So I have looked around and saw a few posts on similar things but they all had to do with geolocation things and all I am trying to do is a simple timer. Basically I am using a TimerTask to serve as a countdown from 30 seconds within an activity class. the app crashes every time that the TimerTask is called. for context purposes this is a child activity from a parent that called it using a startActivityForResult call.
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();
}
}
The logcat shows this
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)
which I took to be a problem with trying to set the text of the view, but that is not the case because after commenting out the set text part it still is wrong
Upvotes: 1
Views: 6192
Reputation: 62411
put yourTaskObj.cancel()
when your task is complete.. eg. task.cancel();
Upvotes: 0
Reputation: 67286
You are trying to update your UI from a non-UI thread, so to do so you need to put your stuff to update the UI inside the runOnUiThread()
Activity_name.this.runOnUiThread(new Runnable() {
public void run() {
text.setText("Level " + Integer.toString(level)+ " will start in " +Integer.toString(time) + " seceonds." );
}
});
Upvotes: 3
Reputation: 411
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." );
}
};
}
You cannot modify the ui from a thread..you can do it with the help of a handler
Upvotes: 0