android - NullPointerException: Attempt to invoke virtual method AlertDialog.setTitle(java.lang.CharSequence) on a null object reference -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
here error:
04-01 10:37:47.077 2310-2326/zonup.asyc d/create response﹕ {"success":"false","msg":"please enter email!!!"} 04-01 10:37:47.084
2310-2310/zonup.asyc d/androidruntime﹕ shutting down vm 04-01 10:37:47.084 2310-2310/zonup.asyc e/androidruntime﹕ fatal exception: main process: zonup.asyc, pid: 2310 java.lang.nullpointerexception: attempt invoke virtual method 'void android.app.alertdialog.settitle(java.lang.charsequence)' on null object reference @ zonup.asyc.mainactivity$signmeup.onpostexecute(mainactivity.java:122) @ zonup.asyc.mainactivity$signmeup.onpostexecute(mainactivity.java:70) @ android.os.asynctask.finish(asynctask.java:632) @ android.os.asynctask.access$600(asynctask.java:177) @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:645) @ 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)
public class mainactivity extends actionbaractivity { protected string stmail; protected edittext edittext; protected button button; alertdialog alertdialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); edittext=(edittext) findviewbyid(r.id.edittext); stmail=edittext.gettext().tostring(); button=(button) findviewbyid(r.id.button); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { new signmeup().execute(); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, 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); } class signmeup extends asynctask<string, string, string> { private progressdialog pdialog; jsonparser jsonparser = new jsonparser(); @override protected void onpreexecute() { super.onpreexecute(); runonuithread(new runnable() { @override public void run() { pdialog = new progressdialog(mainactivity.this); pdialog.setmessage("thanks sign up..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); } }); } @override protected string doinbackground(string... arg0) { // todo auto-generated method stub // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("email", stmail)); // getting json object // note create product url accepts post method jsonobject json = jsonparser.makehttprequest("http://api.php","get", params); // check log cat fro response log.d("create response", json.tostring()); // check success tag try { string success = json.getstring("success"); if (stmail != null & success == "true") { toast.maketext(mainactivity.this, "youre email posted..", toast.length_short).show(); } else { } } catch (jsonexception e) { e.printstacktrace(); } return null; } @suppresswarnings("deprecation") protected void onpostexecute(string file_url) { // dismiss dialog once done edittext.settext(""); alertdialog.settitle("info"); alertdialog.setmessage("you have been subscribed o "); alertdialog.setbutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { // here can add functions } }); alertdialog.show(); } } }
rather using alertdialog use alertdialog.builder. alertdialog deprected. need show progressdialog too.
@override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(mainactivity.this); pdialog.setmessage("thanks sign up..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); }
in onpostexecute
method dismiss
progressbar. use alertdialog.builder.
protected void onpostexecute(string file_url) { // dismiss dialog once done if(pdialog.isshowing()){ pdialog.dismiss(); } edittext.settext(""); alertdailog.builder builder = new alertdialog.builder(activity.this); builder.settext(""); builder.setmessege(""); builder.create().show(); });
Comments
Post a Comment