Change this line...
private Handler myHandler;
to...
private Handler myHandler = new Handler();
You forgot to initialize your muHandler
object. Initialize it.
Update:
why did you put this
in the following line inside your thread???
myHandler.postDelayed(this, 0);
Here, this
means the current Acitivity
's context. Comment out the above line as below...I think your problem will be solved.
private Runnable updateTimerMethod = new Runnable() {
@Override
public void run() {
timeInMillies = SystemClock.uptimeMillis() - startTime;
finalTime = timeSwap + timeInMillies;
int seconds = (int) (finalTime / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (finalTime % 1000);
textTimer.setText("" + minutes + ":" + String.format("%02d", seconds) + ":" + String.format("%03d", milliseconds));
//myHandler.postDelayed(this, 0);
}
};
Another Update:
Initialize your TextView
name textTimer
in onCreate()
method as below...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button)findViewById(R.id.Start);
//initializ as below
textTimer = (TextView)findViewById(R.id.your_text_view);
}
When you are trying set text to that TextView
, it can't find any TextView
.