简体   繁体   中英

Why my stopwatch isn't working in Android Studio?

I am trying to create a stopwatch for my app in Android Studio using this tutorial, and while the app itself is running smoothly, when I press the 'Start' button nothing happens.

Below is my code:

activity_third.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/stopwatch"
    android:textSize="20sp"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/start"/>

    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/stop"/>

</LinearLayout>

<Chronometer
    android:id="@+id/chronometer"
    android:format="%s"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="40sp"/>

ThirdActivity.java:

public class ThirdActivity extends Activity implements OnClickListener {
    private Chronometer chronometer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);

        chronometer = (Chronometer) findViewById(R.id.chronometer);
        findViewById(R.id.start).setOnClickListener(this);
        findViewById(R.id.stop).setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        switch(v.getId()){
            case R.id.start:
                chronometer.setBase(SystemClock.elapsedRealtime());
                chronometer.start();
                break;
            case R.id.stop:
                chronometer.stop();
                break;
        }
    }
}

Can anyone tell me why the buttons aren't working and how to fix them? Thank you!

I used the code you posted above and it works, The only issue was the full layout wasn't copy and pasted.

Check the activity implements View.onClickListener and not another one.

Here is the Layout I used

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Stopwatch"
    android:textSize="20sp"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start"/>

    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop"/>

</LinearLayout>
    <Chronometer
        android:id="@+id/chronometer"
        android:format="%s"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"/>

</LinearLayout>


public class TestActivity extends Activity implements View.OnClickListener {
private Chronometer chronometer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    chronometer = (Chronometer) findViewById(R.id.chronometer);
    findViewById(R.id.start).setOnClickListener(this);
    findViewById(R.id.stop).setOnClickListener(this);
}


@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v){
    switch(v.getId()){
        case R.id.start:
            chronometer.setBase(SystemClock.elapsedRealtime());
            chronometer.start();
            break;
        case R.id.stop:
            chronometer.stop();
            break;
    }
}

}

This code worked an it started the timer displaying to screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM