私の問題は、停止ボタンをクリックした後にファイルが空になることですが、16:49:39 に開始ボタンをクリックしてから 16:49:57 に停止ボタンをクリックすると、時間が変更された場合にのみファイルが空になります。しかし、停止ボタン 16:50:01 をクリックすると、ファイルはデータとともに保存されます。なぜこれが起こるのか教えてください。そして、これを解決する方法。以下に、私のアプリケーションのコードを示します。
オンクリック コード:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.startButton:
stoper.start();
try {
writer = new CSVwriter(dateFormat.format(date));
} catch (IOException e) {
e.printStackTrace();
}
handler.postDelayed(UpdateView, 120);
break;
case R.id.stopButton:
stoper.stop();
try {
writer.closeFile();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
private Runnable UpdateView = new Runnable() {
public void run() {
timerText.setText(stoper.toString());
pulsText.setText(""+accelerationX);
try {
writer.insertLine(new DataObject(stoper.toString(), accelerationX, accelerationY, accelerationZ));
} catch (IOException e) {
e.printStackTrace();
}
handler.postDelayed(UpdateView, 100);
}
};
OnCreate コード:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View startBtn = findViewById(R.id.startButton);
startBtn.setOnClickListener(this);
View stopBtn = findViewById(R.id.stopButton);
stopBtn.setOnClickListener(this);
handler = new Handler();
}