android - AlertDialog in PreferenceFragment crashes app -
i'm trying create alertdialog
in preferencefragment
user can confirm he/she wants delete user data.
this code i've made:
public class settingsfragment extends preferencefragment { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); bundle b = getarguments(); addpreferencesfromresource(r.xml.preferences); preference button = (preference)findpreference(getstring(r.string.delete_user_info)); button.setonpreferenceclicklistener(new preference.onpreferenceclicklistener() { @targetapi(build.version_codes.m) @override public boolean onpreferenceclick(preference preference) { //an alertbuilder new alertdialog.builder(getcontext()) //this line crashes app .setmessage("do want permanently delete likes?\nyou not able restore them!!") .setcancelable(true) .setpositivebutton("yes", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialoginterface, int i) { new deleteuserinfo().execute(); } }) .setnegativebutton("no", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialoginterface, int i) { dialoginterface.cancel(); } }) .create() .show(); return true; } }); } }
and result in logcat:
11-01 18:58:38.501 29821-29821/com.astudio.andreas.mapstest e/androidruntime: fatal exception: main 11-01 18:58:38.501 29821-29821/com.astudio.andreas.mapstest e/androidruntime: process: com.astudio.andreas.mapstest, pid: 29821 11-01 18:58:38.501 29821-29821/com.astudio.andreas.mapstest e/androidruntime: java.lang.nosuchmethoderror: no virtual method getcontext()landroid/content/context; in class lcom/astudio/andreas/mapstest/settingsfragment; or super classes (declaration of 'com.astudio.andreas.mapstest.settingsfragment' appears in /data/app/com.astudio.andreas.mapstest-1/base.apk) 11-01 18:58:38.501 29821-29821/com.astudio.andreas.mapstest e/androidruntime: @ com.astudio.andreas.mapstest.settingsfragment$1.onpreferenceclick(settingsfragment.java:41)
any ideas on how can reference context
correctly?
use getactivity()
instead of getcontext()
.
Comments
Post a Comment