Not sure why but the chronometer counter will start as soon as the application starts without me clicking the button. Does anyone see what the issue would be?

package com.example.android.courtcounter;

import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Chronometer;

public class MainActivity extends AppCompatActivity {

    int scoreTeamA = 0;
    int scoreTeamB = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.setContentView(R.layout.activity_main);
        setContentView(R.layout.activity_main);

    }
//Display team A score
    public void displayForTeamA(int score){
        TextView scoreView = (TextView) findViewById(R.id.team_a_score);
        scoreView.setText(String.valueOf(score));

    }
//Display team B score
    public void displayForTeamB(int score){
        TextView scoreView = (TextView) findViewById(R.id.team_b_score);
        scoreView.setText(String.valueOf(score));

    }

    //Methods for team A button
    public void threePointsButton(View view){
        int threePoints = 3;
        scoreTeamA = scoreTeamA + threePoints;
        displayForTeamA(scoreTeamA);
    }

    public void twoPointsButton (View view){
        int twoPoints = 2;
        scoreTeamA = scoreTeamA + twoPoints;
        displayForTeamA(scoreTeamA);
    }

    public void freeThrowButton (View view){
        int freeThrow = 1;
        scoreTeamA = scoreTeamA + freeThrow;
        displayForTeamA(scoreTeamA);
    }
//Methods for team B button
    public void threePointsButtonTeamB(View view){
        int threePoints = 3;
        scoreTeamB = scoreTeamB + threePoints;
        displayForTeamB(scoreTeamB);
    }

    public void twoPointsButtonTeamB (View view){
        int twoPoints = 2;
        scoreTeamB = scoreTeamB + twoPoints;
        displayForTeamB(scoreTeamB);
    }

    public void freeThrowButtonTeamB (View view){
        int freeThrow = 1;
        scoreTeamB = scoreTeamB + freeThrow;
        displayForTeamB(scoreTeamB);
    }
//Methods for reset button
    public void resetScores (View view){
        int scoreA = 0;
        int scoreB = 0;
        displayForTeamA(scoreA);
        displayForTeamB(scoreB);
        scoreTeamA = 0;
        scoreTeamB = 0;
    }

//Methods for starting timer
    public void startTime (View view){
        Chronometer myC = (Chronometer) findViewById(R.id.mc);
        myC.start();

    }

    //Methods for stopping timer
    public void stopTime (View view){
        Chronometer myC = (Chronometer) findViewById(R.id.mc);
        myC.setBase(SystemClock.elapsedRealtime());
        myC.stop();

    }

}

That code, I can't find where you created the chronometer. Time to look around the rest of the project files.

Hello,

I created the chronometer objectsion in the last two methods.. The methods are buttons in the XML activity_main.

//Methods for starting timer
    public void startTime (View view){
        Chronometer myC = (Chronometer) findViewById(R.id.mc);
        myC.start();
    }
    //Methods for stopping timer
    public void stopTime (View view){
        Chronometer myC = (Chronometer) findViewById(R.id.mc);
        myC.setBase(SystemClock.elapsedRealtime());
        myC.stop();
    }

Is this not creating the chronometer?

Sorry but as I read your reply I only see the methods for starting and stopping, but can't find your code for creating the object.

If I was working this, I'd look again at the object creation and any code with its name.

also: it's good that you show the startTime and stopTime methods, but as far as we can tell, they're not called anywhere.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.