android - Not able to access getPreferences method -
i not able access getpreferences method of sharedpreferences interface in method.i used getpreferences method in method logintofacebook gives me error private_mode how can solve problem.
this edited code.
public class facebookutils { private static final dialoglistener default_auth_activity_code = null; private static string app_id = "783826255024540"; public facebook facebook = new facebook(app_id); private asyncfacebookrunner masyncrunner; string filename = "androidsso_data"; private sharedpreferences mprefs; button btnfblogin; button btnfbgetprofile; context mcontext; public facebookutils() { // todo auto-generated constructor stub } public void authorize(activity activity, string[] permissions, final dialoglistener listener) { authorize(activity, permissions, default_auth_activity_code); } public facebookutils(context context) { } @suppresswarnings("unused") public void logintofacebook() { sharedpreferences sharedpreferences=mcontext.getsharedpreferences("", context.mode_private); string access_token = mprefs.getstring("access_token", null); long expires = mprefs.getlong("access_expires", 0); if (access_token != null) { facebook.setaccesstoken(access_token); btnfblogin.setvisibility(view.invisible); btnfbgetprofile.setvisibility(view.visible); log.d("fb sessions", "" + facebook.issessionvalid()); } if (expires != 0) { facebook.setaccessexpires(expires); } if (!facebook.issessionvalid()) { facebook.authorize((activity) mcontext, new string[] { "email", "publish_stream" }, new dialoglistener() { @override public void oncancel() { } @override public void oncomplete(bundle values) { sharedpreferences sharedpreferences=mcontext.getsharedpreferences("", context.mode_private); editor editor=sharedpreferences.edit(); editor.putstring("access_token",facebook.getaccesstoken()); editor.putlong("access_expires",facebook.getaccessexpires()); editor.commit(); btnfblogin.setvisibility(view.invisible); btnfbgetprofile.setvisibility(view.visible); } @override public void onerror(dialogerror error) { // function handle error } @override public void onfacebookerror(facebookerror fberror) { } }); } } public void getprofileinformation() { masyncrunner.request("me", new requestlistener() { @override public void onmalformedurlexception(malformedurlexception e, object state) { // todo auto-generated method stub } @override public void onioexception(ioexception e, object state) { // todo auto-generated method stub } @override public void onfilenotfoundexception(filenotfoundexception e, object state) { // todo auto-generated method stub } @override public void onfacebookerror(facebookerror e, object state) { // todo auto-generated method stub } @override public void oncomplete(string response, object state) { // todo auto-generated method stub log.d("profile", response); string json = response; try { jsonobject profile = new jsonobject(json); final string name = profile.getstring("name"); final string email = profile.getstring("email"); runonuithread(new runnable() { @override public void run() { toast.maketext(mcontext, "name: " + name + "\nemail: " + email, toast.length_long).show(); } }); } catch (jsonexception e) { e.printstacktrace(); } } }); } protected void runonuithread(runnable runnable) { // todo auto-generated method stub } }
and logcat error.
12-02 13:31:36.648: e/androidruntime(2524): fatal exception: main 12-02 13:31:36.648: e/androidruntime(2524): process: com.facebook.androidhive, pid: 2524 12-02 13:31:36.648: e/androidruntime(2524): java.lang.nullpointerexception: attempt invoke virtual method 'android.content.sharedpreferences android.content.context.getsharedpreferences(java.lang.string, int)' on null object reference 12-02 13:31:36.648: e/androidruntime(2524): @ com.facebook.demo.facebookutils.logintofacebook(facebookutils.java:56) 12-02 13:31:36.648: e/androidruntime(2524): @ com.facebook.demo.androidfacebookconnectactivity$1.onclick(androidfacebookconnectactivity.java:51) 12-02 13:31:36.648: e/androidruntime(2524): @ android.view.view.performclick(view.java:4756) 12-02 13:31:36.648: e/androidruntime(2524): @ android.view.view$performclick.run(view.java:19749) 12-02 13:31:36.648: e/androidruntime(2524): @ android.os.handler.handlecallback(handler.java:739) 12-02 13:31:36.648: e/androidruntime(2524): @ android.os.handler.dispatchmessage(handler.java:95) 12-02 13:31:36.648: e/androidruntime(2524): @ android.os.looper.loop(looper.java:135) 12-02 13:31:36.648: e/androidruntime(2524): @ android.app.activitythread.main(activitythread.java:5221) 12-02 13:31:36.648: e/androidruntime(2524): @ java.lang.reflect.method.invoke(native method) 12-02 13:31:36.648: e/androidruntime(2524): @ java.lang.reflect.method.invoke(method.java:372) 12-02 13:31:36.648: e/androidruntime(2524): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) 12-02 13:31:36.648: e/androidruntime(2524): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694)`
this second activity in called class.
public class androidfacebookconnectactivity extends activity { private static string app_id = "783826255024540"; private facebook facebook = new facebook(app_id); private asyncfacebookrunner masyncrunner; string filename = "androidsso_data"; private sharedpreferences mprefs; button btnfblogin; button btnfbgetprofile; @suppresswarnings("unused") private facebookutils utils; context mcontext; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); btnfblogin = (button) findviewbyid(r.id.btn_fblogin); btnfbgetprofile = (button) findviewbyid(r.id.btn_get_profile); masyncrunner = new asyncfacebookrunner(facebook); utils = new facebookutils(); btnfblogin.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { log.d("image button", "button clicked"); utils.logintofacebook(mcontext); } }); btnfbgetprofile.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { utils.getprofileinformation(); } }); }
error related activity? getting null exception in line. string access_token = mprefs.getstring("access_token", null);
you have not initialized variable mprefs
. hence error since when try mprefs.getstring("access_token", null);
null
;
as of have declared statement,
private sharedpreferences mprefs;
but in-order access have initialize as,
mprefs = getpreferences(mode_private);
before try use it.
Comments
Post a Comment