在null引用上调用虚方法,但为什么它为null?

时间:2016-12-14 21:15:36

标签: java android

所以这是一个天文台,我试图在我的应用程序中实现,但由于计时器中的构建没有显示毫秒,我写了这个,好的想法不是我的,但无论如何:`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();
        }
    }
}
}`

所以上面的代码属于它自己的类。以下是更新包含计时器的textView并位于另一个类

中的方法
    public void updateTimerText(final String time){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
                mTvTime.setText(time);
        }
    });
}

所以问题是Process:com.example.user.myap,PID:6131                   java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'                       在com.example.user.myap.MainActivity $ 2.run(MainActivity.java:105)

0 个答案:

没有答案