java - App crashes when trying to send SMS -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
app crashes when when try send sms application. have app sends sms onclick of button. crashes null pointer exception.
i think has context. don't know how achieve it. i'm new android appreciated.
logcat:
09-12 00:32:25.895 17629-17629/com.nitsystem e/androidruntime: fatal exception: main process: com.nitsystem, pid: 17629 java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string android.content.context.getpackagename()' on null object reference @ android.preference.preferencemanager.getdefaultsharedpreferencesname(preferencemanager.java:499) @ android.preference.preferencemanager.getdefaultsharedpreferences(preferencemanager.java:488) @ com.nitsystem.activity.mainactivity.sendsms(mainactivity.java:208) @ com.nitsystem.activity.mainactivity$1.onclick(mainactivity.java:77) @ android.view.view.performclick(view.java:6213) @ android.widget.textview.performclick(textview.java:11074) @ android.view.view$performclick.run(view.java:23645) @ android.os.handler.handlecallback(handler.java:751) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:154) @ android.app.activitythread.main(activitythread.java:6692) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1468) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1358)
code:
private static final string sms_phone_number="sms"; public static long lastsmssentat = 0; public static string smscontent = "hello world"; public static mainactivity ref= null; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); msendsms = (button) findviewbyid(r.id.start_stop_detection); msendsms.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { sendsms(ref); } }); } public static void sendsms(mainactivity context) { sharedpreferences sharedpref = preferencemanager.getdefaultsharedpreferences(context); string phonenumber = sharedpref.getstring(sms_phone_number, ""); long smsnow = system.currenttimemillis(); long diff = smsnow - lastsmssentat; log.d(tag, "diff : " + diff); log.d(tag, "pre: " + lastsmssentat + " now: " + smsnow); if(diff> 30000){ lastsmssentat = smsnow; calendar calendar = calendar.getinstance(); simpledateformat simpledf = new simpledateformat("yyyy-mm-dd hh:mm:ss"); smsmanager sms = smsmanager.getdefault(); string destinationaddress = phonenumber; string message = smscontent +"\n"; message += "\ntest sms time: " + simpledf.format(calendar.gettime()) + ""; sms.sendtextmessage(destinationaddress, null, message, null,null); log.d(tag,"sms sent"); } } }
try folowing
msendsms.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { sendsms(youractivity.this); } });
Comments
Post a Comment