java - My interface when triggered get NullPointerException -
i try toast message interface. if app not connection internet, want show toast message , i'm wanting java interfaces.
this motheractivity.java
. file implement toastmessagges.toastmessaggecallback
public class motheractivity extends actionbaractivity implements toastmessagges.toastmessaggecallback { toastmessagges toastmessagges; @override protected void onstart() { super.onstart(); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_mother); toastmessagges = new toastmessagges(); appstarter(); } private void appstarter(){ boolean checkinternet = internetcontrol.checkinternetconnection( getapplicationcontext() ); if( checkinternet ) { toastmessagges.show_toast_messagge(); } else { } } @override public void longtoastmessagge() { toast.maketext(getapplicationcontext(), "hello world", toast.length_long).show(); } }
this toastmessagges.java file.
public class toastmessagges { toastmessaggecallback toastmessaggecallback; public void show_toast_messagge(){ toastmessaggecallback.longtoastmessagge(); } public static interface toastmessaggecallback { public void longtoastmessagge(); } }
when start app. nullpointerexception error.
caused by: java.lang.nullpointerexception @ com.medyasef.bulenttirasnewapp.bulenttiras.functions.toastmessagges.show_toast_messagge(toastmessagges.java:22) @ com.medyasef.bulenttirasnewapp.bulenttiras.motheractivity.appstarter(motheractivity.java:36) @ com.medyasef.bulenttirasnewapp.bulenttiras.motheractivity.oncreate(motheractivity.java:29)
toastmessagges.java:22
toastmessaggecallback.longtoastmessagge();
sorry bad english. please help. thank you.
you haven't initialized toastmessaggecallback toastmessaggecallback
.
to this, write
toastmessaggecallback toastmessaggecallback = new toastmessaggecallback(){ public void longtoastmessagge(){ // add toasting code here } };
this make object implementing interface (called "anonymous class"). of course, toastmessaggecallback
should in method longtoastmessagge
, add desired code there.
Comments
Post a Comment