0

I am doing the Score Keeper App, but I decided to add a timer to it. I have looked through codes and tutorials how to add one and I ended up copy/pasting a code into my app. There seem to be no errors found but the app just crashes when I load it and my limited knowledge of Java is preventing me from finding the reason why!

This is my code (not sure where all the imports came from!):

 package com.example.android.footballscorekeeper;

 import android.annotation.SuppressLint;
 import android.annotation.TargetApi;
 import android.app.Activity;
 import android.os.Build;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.view.Window;
 import android.widget.Button;
 import android.widget.TextView;
 import android.os.CountDownTimer;
 import android.view.View.OnClickListener;

 import org.w3c.dom.Text;

 import java.lang.annotation.Target;
 import java.util.concurrent.TimeUnit;

 public class MainActivity extends Activity {

int scoreHome = 0;
int scoreAway = 0;
int onTargetHome = 0;
int onTargetAway = 0;
int offTargetHome = 0;
int offTargetAway = 0;

Button startTime, stopTime, resetTime, resumeTime;
TextView timerText;


    //Declare a variable to hold count down timer's paused status
private boolean isPaused = false;
//Declare a variable to hold count down timer's paused status
private boolean isCanceled = false;

//Declare a variable to hold CountDownTimer remaining time
private long timeRemaining = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    startTime = (Button) findViewById(R.id.startTime);
    stopTime = (Button) findViewById(R.id.stopTime);
    resumeTime = (Button) findViewById(R.id.resumeTime);
    resetTime = (Button) findViewById(R.id.resetTime);
    timerText = (TextView) findViewById(R.id.timerText);

    stopTime.setEnabled(false);
    resumeTime.setEnabled(false);
    resetTime.setEnabled(false);

    timerText.setText("00:20:00");


    //Initially disabled the pause, resume and cancel button
    stopTime.setEnabled(false);
    resumeTime.setEnabled(false);
    resetTime.setEnabled(false);


    //Set a Click Listener for start button
    startTime.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            isPaused = false;
            isCanceled = false;

            //Disable the start and pause button
            startTime.setEnabled(false);
            resumeTime.setEnabled(false);
            //Enabled the pause and cancel button
            stopTime.setEnabled(true);
            resetTime.setEnabled(true);

            CountDownTimer timer;
            long millisInFuture = 30000; //30 seconds
            long countDownInterval = 1000; //1 second


            //Initialize a new CountDownTimer instance
            timer = new CountDownTimer(millisInFuture, countDownInterval) {
                public void onTick(long millisUntilFinished) {
                    //do something in every tick
                    if (isPaused || isCanceled) {
                        //If the user request to cancel or paused the
                        //CountDownTimer we will cancel the current instance
                        cancel();
                    } else {
                        //Display the remaining seconds to app interface
                        //1 second = 1000 milliseconds
                        timerText.setText("" + millisUntilFinished / 1000);
                        //Put count down timer remaining time in a variable
                        timeRemaining = millisUntilFinished;
                    }
                }

                public void onFinish() {
                    //Do something when count down finished
                    timerText.setText("Done");

                    //Enable the start button
                    startTime.setEnabled(true);
                    //Disable the pause, resume and cancel button
                    stopTime.setEnabled(false);
                    resumeTime.setEnabled(false);
                    resetTime.setEnabled(false);
                }
            }.start();
        }
    });

    //Set a Click Listener for pause button
    stopTime.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //When user request to pause the CountDownTimer
            isPaused = true;

            //Enable the resume and cancel button
            resumeTime.setEnabled(true);
            resetTime.setEnabled(true);
            //Disable the start and pause button
            startTime.setEnabled(false);
            stopTime.setEnabled(false);
        }
    });

    //Set a Click Listener for resume button
    resumeTime.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Disable the start and resume button
            startTime.setEnabled(false);
            resumeTime.setEnabled(false);
            //Enable the pause and cancel button
            stopTime.setEnabled(true);
            resetTime.setEnabled(true);

            //Specify the current state is not paused and canceled.
            isPaused = false;
            isCanceled = false;

            //Initialize a new CountDownTimer instance
            long millisInFuture = timeRemaining;
            long countDownInterval = 1000;
            new CountDownTimer(millisInFuture, countDownInterval) {
                public void onTick(long millisUntilFinished) {
                    //Do something in every tick
                    if (isPaused || isCanceled) {
                        //If user requested to pause or cancel the count down timer
                        cancel();
                    } else {
                        timerText.setText("" + millisUntilFinished / 1000);
                        //Put count down timer remaining time in a variable
                        timeRemaining = millisUntilFinished;
                    }
                }

                public void onFinish() {
                    //Do something when count down finished
                    timerText.setText("Done");
                    //Disable the pause, resume and cancel button
                    stopTime.setEnabled(false);
                    resumeTime.setEnabled(false);
                    resetTime.setEnabled(false);
                    //Enable the start button
                    startTime.setEnabled(true);
                }
            }.start();

            //Set a Click Listener for cancel/stop button
            resetTime.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //When user request to cancel the CountDownTimer
                    isCanceled = true;

                    //Disable the cancel, pause and resume button
                    stopTime.setEnabled(false);
                    resumeTime.setEnabled(false);
                    resetTime.setEnabled(false);
                    //Enable the start button
                    startTime.setEnabled(true);

                    //Notify the user that CountDownTimer is canceled/stopped
                    timerText.setText("CountDownTimer Canceled/stopped.");
                }
            });
        }
    });

    //Set a Click Listener for cancel/stop button
    resetTime.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //When user request to cancel the CountDownTimer
            isCanceled = true;

            //Disable the cancel, pause and resume button
            stopTime.setEnabled(false);
            resumeTime.setEnabled(false);
            resetTime.setEnabled(false);
            //Enable the start button
            startTime.setEnabled(true);

            //Notify the user that CountDownTimer is canceled/stopped
            timerText.setText("CountDownTimer Canceled/stopped.");
        }
    });

}


public void displayScoreHome(int score) {
    TextView scoreHome = (TextView) findViewById(R.id.team_a_score);
    scoreHome.setText(String.valueOf(score));
}

public void displayScoreAway(int score) {
    TextView scoreAway = (TextView) findViewById(R.id.team_b_score);
    scoreAway.setText(String.valueOf(score));
}

public void displayOnTargetHome(int score) {
    TextView onTargetHome = (TextView) findViewById(R.id.team_a_ontarget);
    onTargetHome.setText(String.valueOf(score));
}

public void displayOnTargetAway(int score) {
    TextView onTargetAway = (TextView) findViewById(R.id.team_b_ontarget);
    onTargetAway.setText(String.valueOf(score));
}

public void displayOffTargetHome(int score) {
    TextView offTargetHome = (TextView) findViewById(R.id.team_a_offtarget);
    offTargetHome.setText(String.valueOf(score));
}

public void displayOffTargetAway(int score) {
    TextView offTargetAway = (TextView) findViewById(R.id.team_b_offtarget);
    offTargetAway.setText(String.valueOf(score));
}

public void addGoalHome(View v) {
    scoreHome = scoreHome + 1;
    displayScoreHome(scoreHome);
}

public void addGoalAway(View v) {
    scoreAway = scoreAway + 1;
    displayScoreAway(scoreAway);
}

public void addOnTargetHome(View v) {
    onTargetHome = onTargetHome + 1;
    displayOnTargetHome(onTargetHome);
}

public void addOnTargetAway(View v) {
    onTargetAway = onTargetAway + 1;
    displayOnTargetAway(onTargetAway);
}

public void addOffTargetHome(View v) {
    offTargetHome = offTargetHome + 1;
    displayOffTargetHome(offTargetHome);
}

public void addOffTargetAway(View v) {
    offTargetAway = offTargetAway + 1;
    displayOffTargetAway(offTargetAway);
}

public void resetScore(View v) {
    scoreHome = 0;
    scoreAway = 0;
    onTargetHome = 0;
    onTargetAway = 0;
    offTargetHome = 0;
    offTargetAway = 0;
    displayScoreHome(scoreHome);
    displayScoreAway(scoreAway);
    displayOnTargetHome(onTargetHome);
    displayOnTargetAway(onTargetAway);
    displayOffTargetHome(offTargetHome);
    displayOffTargetAway(offTargetAway);
}
}

This is the error log:

07-01 13:42:49.407 21197-21197/com.example.android.footballscorekeeper E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.footballscorekeeper, PID: 21197 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.footballscorekeeper/com.example.android.footballscorekeeper.MainActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) at android.app.ActivityThread.access$1100(ActivityThread.java:221) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7225) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content at com.android.internal.policy.PhoneWindow.requestFeature(PhoneWindow.java:398) at android.app.Activity.requestWindowFeature(Activity.java:3993) at com.example.android.footballscorekeeper.MainActivity.onCreate(MainActivity.java:46) at android.app.Activity.performCreate(Activity.java:6876) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)  at android.app.ActivityThread.access$1100(ActivityThread.java:221)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:158)  at android.app.ActivityThread.main(ActivityThread.java:7225)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)  07-01 13:43:16.977 21197-21197/com.example.android.footballscorekeeper I/Process: Sending signal. PID: 21197 SIG: 9

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

Just like the logs tell you, you should call requestWindowFeature before calling super.onCreate and setContentView.

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);  
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
}

Also look at this question.

Sriram Kailasam
  • 347
  • 3
  • 13