我正在尝试让计时器工作,开始和停止按钮点击事件。我似乎在某个地方引用了空指针,但我在我的代码中找不到问题。如果有人可以帮助我,那就太好了。这是我不断收到的 logcat 错误。我在我的 AndroidManifest 中添加了活动。
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{hva.ktv.getconnected/hva.ktv.getconnected.TestActivity}:java.lang.NullPointerException
public class TestActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
a.setOnClickListener(mStartListener);
b.setOnClickListener(mStopListener);
}
private Handler mHandler = new Handler();
private long mStartTime;
TextView mTimeLabel = (TextView) findViewById(R.id.textView1);
Button a = (Button) findViewById(R.id.button1);
Button b = (Button) findViewById(R.id.button2);
OnClickListener mStartListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (mStartTime == 0L) {
mStartTime = System.currentTimeMillis();
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 100);
}
}
};
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
final long start = mStartTime;
long millis = SystemClock.uptimeMillis() - start;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
if (seconds < 10) {
mTimeLabel.setText("" + minutes + ":0" + seconds);
} else {
mTimeLabel.setText("" + minutes + ":" + seconds);
}
mHandler.postAtTime(this, start
+ (((minutes * 60) + seconds + 1) * 1000));
}
};
OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mHandler.removeCallbacks(mUpdateTimeTask);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test, menu);
return true;
}
}