So this is a chronometer that I am trying to implement in my application but since the build in chronometer doesn't display milliseconds I wrote this one, well the idea is not mine but anyway: `import android.content.Context;
public class Chronometer implements Runnable {
public static final long MILLIS_TO_MINUTES = 60000;
private Context mContext;
private long mStartTime;
private boolean mIsRunning;
public Chronometer(Context context) {
mContext = context;
}
public void start() {
mStartTime = System.currentTimeMillis();
mIsRunning = true;
}
public void stop() {
mIsRunning = false;
}
@Override
public void run() {
while(mIsRunning) {
long since = System.currentTimeMillis() - mStartTime;
int seconds = (int) (since / 1000) % 60;
int minutes = (int) ((since / (MILLIS_TO_MINUTES)) % 60);
int millis = (int) since % 100; //the last 2 digits of millisecs
((MainActivity) mContext).updateTimerText(String.format("%02d:%02d:%03d"
, minutes, seconds, millis));
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}`
So the code above is in a class of it's own. And here is the method that updates the textView that contains the chronometer and which is located in another class
public void updateTimerText(final String time){
runOnUiThread(new Runnable() {
@Override
public void run() {
mTvTime.setText(time);
}
});
}
So the problem is Process: com.example.user.myap, PID: 6131 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.user.myap.MainActivity$2.run(MainActivity.java:105)