java - Android studio crashed when changing intent -


the program should work this, having timer , picture shown, using bottom called "age 10-11" should swap user screen same setup of timer, different pictures being shown. problem crashed when comes intent part of code. works fine if second activity class has default code, when adding same code activity 1 crashes. message "unfortunately, program has stopped working" added log

public class age_7_to_9 extends activity implements onclicklistener {

private countdowntimer countdowntimer; private boolean timerhasstarted = false; private button startb; private button nextb; public textview text; private final long starttime = 30* 1000; private final long interval = 1* 1000;  imageview imageview1; int len=images.length-1; static int curr=0;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_age_7_to_9);      imageview1=(imageview)findviewbyid(r.id.imageview);       startb = (button) this.findviewbyid(r.id.button);     nextb = (button) this.findviewbyid(r.id.teneleven);     startb.setonclicklistener(this);     nextb.setonclicklistener(this);     text = (textview) this.findviewbyid(r.id.timer);     countdowntimer = new mycountdowntimer(starttime, interval);     text.settext(text.gettext() + string.valueof(starttime / 1000)); }     private static final int[] images=new int[]     {r.drawable.p,r.drawable.pp,}; @override public void onclick(view v) {     switch (v.getid())     {         case r.id.button:             if(curr < 2)             {                 imageview1.setimageresource(images[curr]);             }             else             {                 curr=-1;             }             curr++;              if(!timerhasstarted)             {                 countdowntimer.start();                 timerhasstarted = true;                 startb.settext("stop");              }             else             {                 countdowntimer.cancel();                 timerhasstarted = false;                 startb.settext("restart");                  log.i("myactivity", "myclass.getview() — item number " + curr);             }             break;         case r.id.teneleven:             intent = new intent(this, age_10_to_11.class);             startactivity(i);             break;     } }     public class mycountdowntimer extends countdowntimer {     public mycountdowntimer(long starttime, long interval)     {         super(starttime, interval);     }     @override     public void onfinish()     {         text.settext("time's up!");     }      @override     public void ontick(long millisuntilfinished)     {         text.settext(""+ millisuntilfinished / 1000);     } }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.menu_age_7_to_9, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); } 

}

xml code

<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:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".age_7_to_9"     android:background="@drawable/backgroundimage">      <button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="start"         android:id="@+id/button"         android:layout_alignparentbottom="true"         android:layout_centerhorizontal="true"         android:layout_marginbottom="99dp" />      <button         style="?android:attr/buttonstylesmall"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="age 10-11"         android:id="@+id/teneleven"         android:layout_alignparentbottom="true"         android:layout_alignparentright="true"         android:layout_alignparentend="true"         android:onclick="onclick"         />      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textappearance="?android:attr/textappearancelarge"         android:id="@+id/timer"         android:layout_above="@+id/button"         android:layout_centerhorizontal="true"         android:layout_marginbottom="51dp" />      <imageview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/imageview"         android:layout_above="@+id/timer"         android:layout_centerhorizontal="true"         android:layout_marginbottom="50dp" />      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textappearance="?android:attr/textappearancelarge"         android:text="highscore:"         android:id="@+id/textview"         android:layout_alignparenttop="true"         android:layout_alignparentleft="true"         android:layout_alignparentstart="true" />      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textappearance="?android:attr/textappearancelarge"         android:text="0"         android:id="@+id/textview2"         android:layout_aligntop="@+id/textview4"         android:layout_alignright="@+id/teneleven"         android:layout_alignend="@+id/teneleven" />      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textappearance="?android:attr/textappearancelarge"         android:text="counter:"         android:id="@+id/textview3"         android:layout_aligntop="@+id/textview2"         android:layout_torightof="@+id/button"         android:layout_toendof="@+id/button" />      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textappearance="?android:attr/textappearancelarge"         android:text="0"         android:id="@+id/textview4"         android:layout_aligntop="@+id/textview"         android:layout_toleftof="@+id/button"         android:layout_tostartof="@+id/button" />  </relativelayout> 

log below

04-29 10:29:00.390    2198-2198/com.example.daniel.pmp e/androidruntime﹕ fatal exception: main process: com.example.daniel.pmp, pid: 2198 java.lang.runtimeexception: unable start activity    componentinfo{com.example.daniel.pmp/com.example.daniel.pmp.age_10_to_11}: java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.charsequence android.widget.textview.gettext()' on null object reference         @ android.app.activitythread.performlaunchactivity(activitythread.java:2298)         @ android.app.activitythread.handlelaunchactivity(activitythread.java:2360)         @ android.app.activitythread.access$800(activitythread.java:144)         @ android.app.activitythread$h.handlemessage(activitythread.java:1278)         @ android.os.handler.dispatchmessage(handler.java:102)         @ android.os.looper.loop(looper.java:135)         @ android.app.activitythread.main(activitythread.java:5221)         @ java.lang.reflect.method.invoke(native method)         @ java.lang.reflect.method.invoke(method.java:372)         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899)         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.charsequence android.widget.textview.gettext()' on null object reference             @ com.example.daniel.pmp.age_10_to_11.oncreate(age_10_to_11.java:46)             @ android.app.activity.performcreate(activity.java:5937)             @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1105)             @ android.app.activitythread.performlaunchactivity(activitythread.java:2251)             at android.app.activitythread.handlelaunchactivity(activitythread.java:2360)             at android.app.activitythread.access$800(activitythread.java:144)             at android.app.activitythread$h.handlemessage(activitythread.java:1278)             at android.os.handler.dispatchmessage(handler.java:102)             at android.os.looper.loop(looper.java:135)             at android.app.activitythread.main(activitythread.java:5221)             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:899)             at com.android.internal.os.zygoteinit.main(zygoteinit.java:694) 

without seeing logcat difficult know, if code, see things going wrong here. don´t know if copy/paste mistake.

you initializing len before initializing images:

int len=images.length-1; 

after initializing images:

private static final int[] images=new int[]     {r.drawable.p,r.drawable.pp,}; 

that not work, can´t length if images not initialized. leads second mistake:

your images array has comma @ end of list:

 {r.drawable.p,r.drawable.pp,} <-- not work. 

also, using wrong parameter here:

    case r.id.teneleven:             intent = new intent(this, age_10_to_11.class);             startactivity(i);             break; 

instead of "this" must age_7_to_9.this:

     case r.id.teneleven:             intent = new intent(age_7_to_9.this, age_10_to_11.class);             startactivity(i);             break; 

otherwise reference onclicklistener.

because of these 3 mistakes, app not compile. guess causing crash is, don´t have added age_10_to_11 activity manifest.but know exactly, have post logcat...


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -