Abdulrahman Abdulkarim Abdulrahman Abdulkarim - 2 months ago 10
Android Question

Android: app crashing after adding a method to MainActvity

I am working on an app to time and score basketball games, when I added the countdown part and a button to control the count to the

MainActivity.java
file, the app builds fine but crashes every time. Here are codes I used.
Here is the error generated every time i try to launch the app

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.abdulkarim.courtcounter, PID: 19740
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.abdulkarim.courtcounter/com.example.abdulkarim.courtcounter.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:116)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:50)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.abdulkarim.courtcounter.MainActivity.<init>(MainActivity.java:14)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471) 
at android.app.ActivityThread.access$900(ActivityThread.java:175) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:146) 
at android.app.ActivityThread.main(ActivityThread.java:5603) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
at dalvik.system.NativeStart.main(Native Method)


MainActivity.java

package com.example.abdulkarim.courtcounter;

import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

int scoreTeamA = 0, scoreTeamB = 0;
TextView minutes = (TextView) findViewById(R.id.min_counter);
TextView seconds = (TextView) findViewById(R.id.sec_counter);
long milliLeft, min, sec;
CountDownTimer gameTime;
final Button timeoutButton = (Button) findViewById(R.id.timeout_button);

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

/**
* This method is called when the +3 points button is clicked
*/
public void threePointsA(View view){
scoreTeamA = scoreTeamA + 3;
displayScoreA(scoreTeamA);
}

/**
* This method is called when the +2 points button is clicked
*/
public void twoPointsA(View view){
scoreTeamA = scoreTeamA + 2;
displayScoreA(scoreTeamA);
}

/**
* This method is called when the Free Throw button is clicked
*/
public void freeThrowA(View view){
scoreTeamA = scoreTeamA + 1;
displayScoreA(scoreTeamA);
}

/**
* This method is called when the +3 points button is clicked
*/
public void threePointsB(View view){
scoreTeamB = scoreTeamB + 3;
displayScoreB(scoreTeamB);
}

/**
* This method is called when the +2 points button is clicked
*/
public void twoPointsB(View view){
scoreTeamB = scoreTeamB + 2;
displayScoreB(scoreTeamB);
}

/**
* This method is called when the Free Throw button is clicked
*/
public void freeThrowB(View view){
scoreTeamB = scoreTeamB + 1;
displayScoreB(scoreTeamB);
}

/**
* This method is called when the reset button is clicked
*/
public void reset(View view){
scoreTeamA = 0;
scoreTeamB = 0;
displayScoreA(scoreTeamA);
displayScoreB(scoreTeamB);
}

/**
* This method initializes the countdown timer
*/
public void gameTime(long timeLengthMilli) {
gameTime = new CountDownTimer(timeLengthMilli, 1000) {

@Override
public void onTick(long milliTillFinish) {
milliLeft = milliTillFinish;
min = (milliTillFinish / (1000 * 60));
sec = ((milliTillFinish / 1000) - min * 60);
minutes.setText(Long.toString(min));
seconds.setText(Long.toString(sec));
Log.i("Tick", "Tock");
}
public void onFinish() {
minutes.setText("00");
seconds.setText("00");
}
}.start();

}

public void timerPause() {
gameTime.cancel();
}

private void timerResume() {
Log.i("min", Long.toString(min));
Log.i("Sec", Long.toString(sec));
gameTime(milliLeft);
}

/**
*This method is called when the timeOut button is clicked
*/
public void timeOut(View view){
timeoutButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(timeoutButton.getText().equals("Start")){
Log.i("Started", timeoutButton.getText().toString());
timeoutButton.setText("Timeout");
gameTime(60*1000);
} else if (timeoutButton.getText().equals("Pause")){
Log.i("Timeout", timeoutButton.getText().toString());
timeoutButton.setText("Resume");
timerPause();
} else if (timeoutButton.getText().equals("Resume")){
timeoutButton.setText("Timeout");
timerResume();
}
}
});
}
/**
* This method handles the display of scores for team A
*/
private void displayScoreA(int score){
TextView scoreTextView = (TextView) findViewById(R.id.teamA_score_text_view);
scoreTextView.setText(String.valueOf(score));
}
/**
* This method handles the display of scores for team A
*/
private void displayScoreB(int score){
TextView scoreTextView = (TextView) findViewById(R.id.teamB_score_text_view);
scoreTextView.setText(String.valueOf(score));
}
}


main_activity.xml

<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abdulkarim.courtcounter.MainActivity">

<RelativeLayout
android:id="@+id/r1_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<TextView
android:id="@+id/min_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="75dp"
android:paddingTop="16dp"
android:text="Mins"
android:textAllCaps="true"
android:textColor="@android:color/black"
android:textSize="10sp" />

<TextView
android:id="@+id/sec_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingTop="16dp"
android:layout_toRightOf="@id/min_text_view"
android:text="Secs"
android:textAllCaps="true"
android:textColor="@android:color/black"
android:textSize="10sp" />

<TextView
android:id="@+id/min_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/min_text_view"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif-light"
android:text="00"
android:textColor="#000000"
android:textSize="56sp" />

<TextView
android:id="@+id/blinker_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/min_text_view"
android:layout_marginTop="8dp"
android:layout_toRightOf="@id/min_counter"
android:fontFamily="sans-serif-light"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text=":"
android:textColor="#000000"
android:textSize="56sp" />

<TextView
android:id="@+id/sec_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/min_text_view"
android:layout_marginTop="8dp"
android:layout_toRightOf="@id/blinker_text_view"
android:fontFamily="sans-serif-light"
android:text="00"
android:textColor="#000000"
android:textSize="56sp" />
</RelativeLayout>

<LinearLayout
android:id="@+id/l1_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/r1_layout"
android:orientation="horizontal">

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:text="Team A"
android:textColor="#616161"
android:textSize="14sp" />

<TextView
android:id="@+id/teamA_score_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="24dp"
android:fontFamily="sans-serif-medium"
android:onClick="threePointsA"
android:text="+3 Points"
android:textAllCaps="true"
android:textColor="#616161"
android:textSize="14sp" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:fontFamily="sans-serif-medium"
android:onClick="twoPointsA"
android:text="+2 Points"
android:textAllCaps="true"
android:textColor="#616161"
android:textSize="14sp" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:fontFamily="sans-serif-medium"
android:onClick="freeThrowA"
android:text="Free Throw"
android:textAllCaps="true"
android:textColor="#616161"
android:textSize="14sp" />
</LinearLayout>

<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:background="@android:color/darker_gray"></View>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"

android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:text="Team B"
android:textColor="#616161"
android:textSize="14sp" />

<TextView
android:id="@+id/teamB_score_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="24dp"
android:fontFamily="sans-serif-medium"
android:onClick="threePointsB"
android:text="+3 Points"
android:textAllCaps="true"
android:textColor="#616161"
android:textSize="14sp" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:fontFamily="sans-serif-medium"
android:onClick="twoPointsB"
android:text="+2 Points"
android:textAllCaps="true"
android:textColor="#616161"
android:textSize="14sp" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:fontFamily="sans-serif-medium"
android:onClick="freeThrowB"
android:text="Free Throw"
android:textAllCaps="true"
android:textColor="#616161"
android:textSize="14sp" />
</LinearLayout>

</LinearLayout>


<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<Button
android:id="@+id/timeout_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginBottom="32dp"
android:layout_marginLeft="16dp"
android:text="Start"
android:fontFamily="sans-serif-medium"
android:textAllCaps="true"
android:textColor="#616161"
android:textSize="14sp"
android:onClick="timeOut"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:layout_marginRight="16dp"
android:layout_marginLeft="8dp"
android:fontFamily="sans-serif-medium"
android:onClick="Reset"
android:text="Reset"
android:textAllCaps="true"
android:textColor="#616161"
android:textSize="14sp" />
</LinearLayout>

</RelativeLayout>

Answer

You can't call findViewById before the onCreate method.
Move the initialization of your view references inside onCreate.

public class MainActivity extends AppCompatActivity {

    int scoreTeamA = 0, scoreTeamB = 0;
    TextView minutes; 
    TextView seconds; 
    long milliLeft, min, sec;
    CountDownTimer gameTime;
    Button timeoutButton;

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

        minutes = (TextView) findViewById(R.id.min_counter);
        seconds = (TextView) findViewById(R.id.sec_counter);
        timeoutButton = (Button) findViewById(R.id.timeout_button);
    }