java - I am trying to launch app and it is crashing -
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 instantiate activity componentinfo{com.example.android.cricketscore/com.example.android.cricketscore.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'android.view.window$callback android.view.window.getcallback()' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2366) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2517) @ android.app.activitythread.access$800(activitythread.java:162) @ android.app.activitythread$h.handlemessage(activitythread.java:1412) @ android.os.handler.dispatchmessage(handler.java:106) @ android.os.looper.loop(looper.java:189) @ android.app.activitythread.main(activitythread.java:5529) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:950) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:745) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.view.window$callback android.view.window.getcallback()' on null object reference @ android.support.v7.app.appcompatdelegateimplbase.<init>(appcompatdelegateimplbase.java:116) @ android.support.v7.app.appcompatdelegateimplv9.<init>(appcompatdelegateimplv9.java:147) @ android.support.v7.app.appcompatdelegateimplv11.<init>(appcompatdelegateimplv11.java:27) @ android.support.v7.app.appcompatdelegateimplv14.<init>(appcompatdelegateimplv14.java:50) @ android.support.v7.app.appcompatdelegate.create(appcompatdelegate.java:201) @ android.support.v7.app.appcompatdelegate.create(appcompatdelegate.java:181) @ android.support.v7.app.appcompatactivity.getdelegate(appcompatactivity.java:521) @ android.support.v7.app.appcompatactivity.findviewbyid(appcompatactivity.java:190) @ com.example.android.cricketscore.mainactivity.<init>(mainactivity.java:19) @ java.lang.reflect.constructor.newinstance(native method) @ java.lang.class.newinstance(class.java:1572) @ android.app.instrumentation.newactivity(instrumentation.java:1065) @ android.app.activitythread.performlaunchactivity(activitythread.java:2356) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2517) @ android.app.activitythread.access$800(activitythread.java:162) @ android.app.activitythread$h.handlemessage(activitythread.java:1412) @ android.os.handler.dispatchmessage(handler.java:106) @ android.os.looper.loop(looper.java:189) @ android.app.activitythread.main(activitythread.java:5529) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:950) @ 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 has scored 1 run checkbox oneruncheckbox = (checkbox) findviewbyid(r.id.addoneforteama); boolean isonerun = oneruncheckbox.ischecked(); /**calculates run of score. * * @return total run */ //add 1 score if team scored 1 run public void addoneforteama(view, view){ scoreteama = scoreteama + 1; displayforteama(scoreteama); } /** method creates summary of total score * @param addonerun scored * @param score summary */ /**this method displays score on 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>
you're running nullpointerexception
on line 19, explained in logcat:
caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.view.window$callback android.view.window.getcallback()' on null object reference ... @ com.example.android.cricketscore.mainactivity.<init>(mainactivity.java:19)
the lines:
checkbox oneruncheckbox = (checkbox) findviewbyid(r.id.addoneforteama); boolean isonerun = oneruncheckbox.ischecked();
need inside oncreate()
method, 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(); }
Comments
Post a Comment