我正在创建一个简单的倒计时计时器,该倒计时计时器从单击按钮开始,然后在单击按钮时暂停。倒计时开始但没有暂停,当我单击按钮暂停时,应用程序崩溃。 这是我的代码
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
CountDownTimer timer;
boolean validate;
Button b1;
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
long totalMilliseconds = 90000;
long interval = 1;
b1 = (Button)findViewById(R.id.push_button);
validate = false;
t1 = (TextView)findViewById(R.id.txtview1);
timer = new CountDownTimer(40000, 1000) {
@Override
public void onTick(long l) {
t1.setText(""+(l));
}
@Override
public void onFinish() {
t1.setText("Finish");
}
};
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(validate){
validate = false;
try {
timer.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(!validate){
validate = true;
timer.start();
}
}
});
}
}
我在logcat中遇到错误
java.lang.IllegalMonitorStateException: object not locked by thread before wait()
at java.lang.Object.wait(Native Method)
at com.example.satnamsingh.countdowntimer.MainActivity$2$override.onClick(MainActivity.java:44)
代码有什么问题。我应该在代码中替换或添加一些内容,以实现简单的暂停。
答案 0 :(得分:0)
> This is a simple Stopwatch App code where Stop and Start button both work fine.
Hope this will help you
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--<Chronometer-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:id="@+id/chronometer"-->
<!--android:textAlignment="center"-->
<!--android:layout_margin="16dp"-->
<!--android:textAppearance="@style/TextAppearance.AppCompat.Large"-->
<!--android:textColor="@color/colorPrimaryDark"-->
<!--android:background="@color/colorAccent"/>-->
<!--<Button-->
<!--android:layout_marginTop="40dp"-->
<!--android:layout_marginStart="150dp"-->
<!--android:layout_below="@+id/chronometer"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="start"-->
<!--android:id="@+id/start"/>-->
<!--<Button-->
<!--android:visibility="invisible"-->
<!--android:layout_marginTop="40dp"-->
<!--android:layout_marginStart="150dp"-->
<!--android:layout_below="@+id/start"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="stop"-->
<!--android:id="@+id/stop"-->
<!--android:background="@drawable/ovel"/>-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginTop="40dp"
android:text="Stop Watch"
android:textSize="20dp" />
<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginTop="40dp"
android:textSize="20dp" />
<Button
android:id="@+id/startBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginTop="40dp"
android:text="Start" />
<Button
android:id="@+id/pauseBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginTop="40dp"
android:text="Pause"
android:visibility="gone" />
<Button
android:id="@+id/resetBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginTop="40dp"
android:text="Reset" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {
private Chronometer chronometer1;
private boolean isStart;
Chronometer chronometer;
Button startBtn, pauseBtn, resetBtn;
long stopTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chronometer1 = (Chronometer) findViewById(R.id.chronometer);
chronometer = (Chronometer)findViewById(R.id.chronometer);
startBtn = (Button)findViewById(R.id.startBtn);
pauseBtn = (Button)findViewById(R.id.pauseBtn);
resetBtn = (Button)findViewById(R.id.resetBtn);
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime() + stopTime);
chronometer.start();
startBtn.setVisibility(View.GONE);
pauseBtn.setVisibility(View.VISIBLE);
}
});
pauseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopTime = chronometer.getBase() - SystemClock.elapsedRealtime();
chronometer.stop();
startBtn.setVisibility(View.VISIBLE);
pauseBtn.setVisibility(View.GONE);
}
});
resetBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
stopTime = 0;
chronometer.stop();
startBtn.setVisibility(View.VISIBLE);
pauseBtn.setVisibility(View.GONE);
}
});
}