java - object oriented programming in android -


i want use method in 1 of classes in class , code doesn't have error when run project "unfortunately project * hast stopped" error. , when copy method instead of creating object , on run program without problem , can 1 tell me may problem ?

public void onclick(view arg0) {      string p = "+989357835774";     string m = "test";     sendsms sms = new sendsms();     sms.sms(p, m); }   public class sendsms extends activity {      public void sms (string phonno , string message){         pendingintent pi = pendingintent.getactivity(this, 0,             new intent(this, sendsms.class), 0);                         smsmanager sms = smsmanager.getdefault();         sms.sendtextmessage(phonno, "+9891100500", message , pi, null);       } } 

these errors :

08-23 19:31:00.065: e/androidruntime(9985): fatal exception: main 08-23 19:31:00.065: e/androidruntime(9985): java.lang.nullpointerexception 08-23 19:31:00.065: e/androidruntime(9985):     @ android.content.contextwrapper.getpackagename(contextwrapper.java:127) 08-23 19:31:00.065: e/androidruntime(9985):     @ android.content.componentname.<init>(componentname.java:75) 08-23 19:31:00.065: e/androidruntime(9985):     @ android.content.intent.<init>(intent.java:3655) 08-23 19:31:00.065: e/androidruntime(9985):     @ com.example.finalproject3.sendsms.sms(sendsms.java:15) 08-23 19:31:00.065: e/androidruntime(9985):     @ com.example.finalproject3.pizza$1$2.onclick(pizza.java:53) 08-23 19:31:00.065: e/androidruntime(9985):     @ android.view.view.performclick(view.java:4101) 08-23 19:31:00.065: e/androidruntime(9985):     @ android.view.view$performclick.run(view.java:17078) 08-23 19:31:00.065: e/androidruntime(9985):     @ android.os.handler.handlecallback(handler.java:615) 08-23 19:31:00.065: e/androidruntime(9985):     @ android.os.handler.dispatchmessage(handler.java:92) 08-23 19:31:00.065: e/androidruntime(9985):     @ android.os.looper.loop(looper.java:155) 08-23 19:31:00.065: e/androidruntime(9985):     @ android.app.activitythread.main(activitythread.java:5485) 08-23 19:31:00.065: e/androidruntime(9985):     @ java.lang.reflect.method.invokenative(native method) 08-23 19:31:00.065: e/androidruntime(9985):     @ java.lang.reflect.method.invoke(method.java:511) 08-23 19:31:00.065: e/androidruntime(9985):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1028) 08-23 19:31:00.065: e/androidruntime(9985):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:795) 08-23 19:31:00.065: e/androidruntime(9985):     @ dalvik.system.nativestart.main(native method) 

your sendsms class activity. should never instantiate activity yourself, because none of life cycle methods called then. let system start activities (using intents).

if need normal class instantiate don't inherit activity.

to access methods found in activity or context pass in valid context in class constructor.

assuming onclick method defined in activity:

public void onclick(view arg0) {      string p = "+989357835774";     string m = "test";     sendsms sms = new sendsms(this);     sms.sms(p, m); }   public class sendsms {      private context context;      public sendsms(context context) {         this.context = context;     }      public void sms (string phonno , string message){         pendingintent pi = pendingintent.getactivity(context, 0,             new intent(context, youractivity.class), 0);                         smsmanager sms = smsmanager.getdefault();         sms.sendtextmessage(phonno, "+9891100500", message , pi, null);       } } 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -