java - OnClickListener throws NullPointerException on startActivity -


i using android api 17 android development tools (adt) have help buttton listening click event. helpbuttonlistener listening click event help button following:

excerpt mainactivity.java

private final class helpbuttonlistener implements onclicklistener {     public void onclick(view v) {                    intent maintohelpintent = new intent(mainactivity.this, helpactivity.class);         startactivity(maintohelpintent);             } } 

following code helpactivity works fine ans shows me some helpful stuff.. on helpactivity creation:

public class helpactivity extends roboactivity {     @override     protected void oncreate(bundle savedinstancestate) {         textview helptextview = new textview(helpactivity.this);         helptextview.settext("some helpful stuff..");         setcontentview(helptextview);     } } 

but if try use set text in textview view component injected using roboguice 2 inject textview using following code, gives me nullpointerexception:

helpactivity.java

public class helpactivity extends roboactivity {     @injectview(r.id.helptextview)     private textview helptextview;      protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         helptextview.settext("some helpful stuff..");         setcontentview(r.layout.activity_help);     } } 

following error when click button , try set text in textview created on activity_help.xml layout resource:

05-16 18:00:52.591: e/androidruntime(9880): fatal exception: main 05-16 18:00:52.591: e/androidruntime(9880): java.lang.runtimeexception: unable start activity componentinfo{com.wickedlynotsmart.myfirstapp/com.wickedlynotsmart.myfirstapp.activity.helpactivity}: java.lang.nullpointerexception 05-16 18:00:52.591: e/androidruntime(9880):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 05-16 18:00:52.591: e/androidruntime(9880):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 05-16 18:00:52.591: e/androidruntime(9880):     @ android.app.activitythread.access$600(activitythread.java:141) 05-16 18:00:52.591: e/androidruntime(9880):     @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 05-16 18:00:52.591: e/androidruntime(9880):     @ android.os.handler.dispatchmessage(handler.java:99) 05-16 18:00:52.591: e/androidruntime(9880):     @ android.os.looper.loop(looper.java:137) 05-16 18:00:52.591: e/androidruntime(9880):     @ android.app.activitythread.main(activitythread.java:5041) 05-16 18:00:52.591: e/androidruntime(9880):     @ java.lang.reflect.method.invokenative(native method) 05-16 18:00:52.591: e/androidruntime(9880):     @ java.lang.reflect.method.invoke(method.java:511) 05-16 18:00:52.591: e/androidruntime(9880):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 05-16 18:00:52.591: e/androidruntime(9880):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 05-16 18:00:52.591: e/androidruntime(9880):     @ dalvik.system.nativestart.main(native method) 05-16 18:00:52.591: e/androidruntime(9880): caused by: java.lang.nullpointerexception 05-16 18:00:52.591: e/androidruntime(9880):     @ com.wickedlynotsmart.myfirstapp.activity.helpactivity.oncreate(helpactivity.java:22) 05-16 18:00:52.591: e/androidruntime(9880):     @ android.app.activity.performcreate(activity.java:5104) 05-16 18:00:52.591: e/androidruntime(9880):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 05-16 18:00:52.591: e/androidruntime(9880):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 05-16 18:00:52.591: e/androidruntime(9880):     ... 11 more 

could me understand why getting error?

thanks.

first set content activity. initialize textview. set text textview. have not initialized textview. hence got nullpointerexception.

it should like:

setcontentview(r.layout.activity_help); textview helptextview = (textview)findviewbyid(r.id.helptextview); helptextview.settext("some helpful stuff.."); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -