android - App keeps stopping when i press a button -
i have save button loads dialog interface asking save name. when click done it's supposed created sharedpreferences strings in it, instead says unfortunately program has stopped. how can fix this? oh , want put out there i'm new android. i'm learning of stuff make app.
public void save(view view){ layoutinflater li = layoutinflater.from(calculate.this); view pview = li.inflate(r.layout.prompt, null); alertdialog.builder adb = new alertdialog.builder(calculate.this); adb.setview(pview); final edittext name = (edittext) findviewbyid(r.id.etprompt); adb.setcancelable(false); adb.setpositivebutton("done", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { n = name.gettext().tostring(); try { details = getsharedpreferences(n,mode_private); editor = details.edit(); editor.putstring("ebp", ebp.gettext().tostring()); editor.putstring("fb", fb.gettext().tostring()); editor.putstring("sv", sv.gettext().tostring()); editor.putstring("pv", pv.gettext().tostring()); editor.putstring("sl", sl.gettext().tostring()); editor.putstring("cl", cl.gettext().tostring()); editor.commit(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } }); adb.setnegativebutton("cancel", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); alertdialog ad = adb.create(); ad.show(); }
logcat
05-18 20:54:28.300: e/androidruntime(16575): fatal exception: main 05-18 20:54:28.300: e/androidruntime(16575): java.lang.nullpointerexception 05-18 20:54:28.300: e/androidruntime(16575): @ com.tbjsoft.enclosurecalculator.calculate$1.onclick(calculate.java:103) 05-18 20:54:28.300: e/androidruntime(16575): @ com.android.internal.app.alertcontroller$buttonhandler.handlemessage(alertcontroller.java:166) 05-18 20:54:28.300: e/androidruntime(16575): @ android.os.handler.dispatchmessage(handler.java:99) 05-18 20:54:28.300: e/androidruntime(16575): @ android.os.looper.loop(looper.java:137) 05-18 20:54:28.300: e/androidruntime(16575): @ android.app.activitythread.main(activitythread.java:5041) 05-18 20:54:28.300: e/androidruntime(16575): @ java.lang.reflect.method.invokenative(native method) 05-18 20:54:28.300: e/androidruntime(16575): @ java.lang.reflect.method.invoke(method.java:511) 05-18 20:54:28.300: e/androidruntime(16575): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 05-18 20:54:28.300: e/androidruntime(16575): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 05-18 20:54:28.300: e/androidruntime(16575): @ dalvik.system.nativestart.main(native method)
you need use activities context
access sharedpreferences
inside alertdialog
. can either initialize them outside of alertdialog
or change to
details = caluclate.this.getsharedpreferences(n,mode_private); // assuming calculate name of `activity`
if doesn't please post logcat know crashing
edit
change
final edittext name = (edittext) findviewbyid(r.id.etprompt);
to
final edittext name = (edittext) pview.findviewbyid(r.id.etprompt);
Comments
Post a Comment