-2

I'm getting a error when I hit my start button for my timer. I don't know why its not working. I had it working with this code before but now when I try and run it it just immediately crashes with the error.

Here is the class for my timer code:

package com.example.pc.mealtimers;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;

public class WegmansActivity extends AppCompatActivity{
    static TextView wegmansTimerText;
    static Boolean counterIsActive = false;
    Button wegStart;
    static CountDownTimer countDownTimer;




    public static void resetTimer(){
        wegmansTimerText.setText("16:00");
        countDownTimer.cancel();
        counterIsActive = false;

    }

    public static void wegmansTimerInit() {
        if (counterIsActive == false) {
            counterIsActive = true;
            //wegButton.setText("Stop!");
            countDownTimer = new CountDownTimer(960000, 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    updateTimer((int) millisUntilFinished / 1000);
                }

                @Override
                public void onFinish() {
                   wegmansTimerText.setText("0:00");
                }
            }.start();
        }else{
            wegmansTimerText.setText("16:00");
            resetTimer();

        }

    }

    public static void updateTimer(int secondsLeft){

        //System.out.println("TICKING");
        counterIsActive = true;
        int minutes = (int) secondsLeft / 60;
        int seconds = secondsLeft - minutes *60;
        String secondString = Integer.toString(seconds);
        if (seconds <=9){
            secondString = "0" + secondString;

        }
        wegmansTimerText.setText(Integer.toString(minutes) + ":" + secondString);

    }

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

        wegmansTimerText = (TextView)findViewById(R.id.wegmansTimerText);
        wegStart = (Button)findViewById(R.id.startButton);
    }
}

Here is my code for the layout in which the timer is located:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".WegmansActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Back"
        android:id="@+id/backButton"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="backButton" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="400°"
        android:id="@+id/degreeText"
        android:textSize="60sp"
        android:layout_below="@+id/backButton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="16:00"
        android:id="@+id/wegmansTimerText"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textSize="60sp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start"
        android:id="@+id/startButton"
        android:layout_marginBottom="52dp"
        android:textSize="20sp"
        android:padding="20dp"
        android:onClick="wegmansTimer"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

My error::

java.lang.IllegalArgumentException: Binary XML file line #44: Process: com.example.pc.mealtimers, PID: 2346
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at com.example.pc.mealtimers.WegmansActivity.updateTimer(WegmansActivity.java:60)
    at com.example.pc.mealtimers.WegmansActivity$1.onTick(WegmansActivity.java:33)
    at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:133)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
DF_Waffle
  • 1
  • 1

1 Answers1

0

This is just an educated guess but the NPE might occur due to the fact that you call a method on the field wegmansTimerText in the static method updateTimer.

Guess updateTimerthrough the init method gets executed before onCreate is called which instantiates the particular field. This way wegmansTimerTextis null. You've got to allign the called methods for your timer correctly with the lifecycle - meaning you might need to put content of the static methods into onCreate or on of the following lifecycle states.

I don't have Android Studio here right now, so this isn't tested but might point you in the right direction.

Hope this helps!