Reputation: 13
Error:
01-14 20:59:18.266 27103-27103/com.example.android.cricketscore E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.cricketscore, PID: 27103
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.cricketscore/com.example.android.cricketscore.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2366)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2517)
at android.app.ActivityThread.access$800(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:189)
at android.app.ActivityThread.main(ActivityThread.java:5529)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
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.android.cricketscore.MainActivity.<init>(MainActivity.java:19)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2356)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2517)
at android.app.ActivityThread.access$800(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:189)
at android.app.ActivityThread.main(ActivityThread.java:5529)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
MainActivity. java
package com.example.android.cricketscore;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//Figure out if Team A has scored One Run
CheckBox oneRunCheckbox = (CheckBox) findViewById(R.id.addOneForTeamA);
boolean isOneRun = oneRunCheckbox.isChecked();
/**Calculates the run of the score.
*
* @return total run
*/
//Add 1 to the score if team A scored 1 run
public void addOneForTeamA(View, view){
scoreTeamA = scoreTeamA + 1;
displayForTeamA(scoreTeamA);
}
/** This method creates a summary of the total score
* @param addOneRun is scored
* @param score summary
*/
/**This method displays the score on the screen. */
public void displayForTeamA(int score){
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
}
activity_main.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.android.cricketscore.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:text="Team A"
android:textSize="14sp"
android:id="@+id/textView2" />
<TextView
android:id="@+id/team_a_score"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:text="0/0"
android:textSize="56sp"
android:layout_marginTop="15dp"
android:layout_alignTop="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<CheckBox
android:id="@+id/addOneForTeamA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:text="1 Run"
android:textSize="16sp"
android:layout_below="@+id/team_a_score"
android:layout_margin="16dp" />
</RelativeLayout>
Upvotes: 1
Views: 94
Reputation: 24211
The NullPointerException
is already spotted in previous answers. Here's the erroneous function you've here.
Just remove the View
parameter as you're not using it in this function.
//Add 1 to the score if team A scored 1 run
public void addOneForTeamA(){
scoreTeamA = scoreTeamA + 1;
displayForTeamA(scoreTeamA);
}
Anyway, if you wanted to pass the view
to the addOneForTeamA()
function, you could do something like this.
//Add 1 to the score if team A scored 1 run
public void addOneForTeamA(View view){ // remove the comma
scoreTeamA = scoreTeamA + 1;
displayForTeamA(scoreTeamA);
}
Upvotes: 0
Reputation: 6114
Try this:
package com.example.android.cricketscore;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckBox oneRunCheckbox = (CheckBox) findViewById(R.id.addOneForTeamA);
boolean isOneRun = oneRunCheckbox.isChecked();
}
//Figure out if Team A has scored One Run
/**Calculates the run of the score.
*
* @return total run
*/
//Add 1 to the score if team A scored 1 run
public void addOneForTeamA(View, view){
scoreTeamA = scoreTeamA + 1;
displayForTeamA(scoreTeamA);
}
/** This method creates a summary of the total score
* @param addOneRun is scored
* @param score summary
*/
/**This method displays the score on the screen. */
public void displayForTeamA(int score){
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
}
Upvotes: 0
Reputation: 82563
You're running into a NullPointerException
on line 19, as explained in the LogCat:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
...
at com.example.android.cricketscore.MainActivity.<init>(MainActivity.java:19)
The lines:
CheckBox oneRunCheckbox = (CheckBox) findViewById(R.id.addOneForTeamA);
boolean isOneRun = oneRunCheckbox.isChecked();
Need to be inside your onCreate()
method, like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckBox oneRunCheckbox = (CheckBox) findViewById(R.id.addOneForTeamA);
boolean isOneRun = oneRunCheckbox.isChecked();
}
Upvotes: 3