java - Android; How to return a method for value from an activity inside a fragment? -


i realy new in android make lot of mistakes. basicly following tutorials. problem in question let me explain anyways.

basicly have class use store user details shared preferences. , trying user details show in profile layout fragment. , before writing user details profile fragment chechk if user logged in.

but while checking if user logged, app crashes, think because not calling method in right way.

here code problematic section shown ;

    public class userprofile extends android.support.v4.app.fragment implements view.onclicklistener{     view rootview;     button logoutbutton;     edittext regusername, regpassword, regemail, regphonenumber, regbloodtype, regbirthday, regaddress;     userlocalstore userlocalstore;     context context;      @nullable     @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {         rootview = inflater.inflate(r.layout.userprofile_layout,container,false);         regusername = (edittext)rootview.findviewbyid(r.id.regusername);         regpassword = (edittext)rootview.findviewbyid(r.id.regpassword);         regemail = (edittext)rootview.findviewbyid(r.id.regemail);         regphonenumber = (edittext)rootview.findviewbyid(r.id.regphonenumber);         regbloodtype = (edittext)rootview.findviewbyid(r.id.regbloodtype);         regbirthday = (edittext)rootview.findviewbyid(r.id.regbirthday);         regaddress = (edittext)rootview.findviewbyid(r.id.regaddress);          logoutbutton = (button)rootview.findviewbyid(r.id.logoutbutton);         logoutbutton.setonclicklistener(this);             return rootview;     }      @override     public void onstart() {         super.onstart();          if (authenticate()== true) //*(crashes here)         {             displayuserdetails();         }         else{             intent intent = new intent(getactivity(),loginscreen.class);             startactivity(intent);         }     }     private void displayuserdetails()     {         user user = userlocalstore.getloggedinuser();         regusername.settext(user.username);         regemail.settext(user.email);         regphonenumber.settext(user.phonenumber);         regbloodtype.settext(user.bloodtype);         regaddress.settext(user.address);     }      private boolean authenticate()     {         return userlocalstore.getuserloogedin();// none of these seems work         startactivityforresult(userlocalstore.getloggedinuser(),boolean);         return ((userlocalstore)getactivity()).getloggedinuser();     }     @override     public void onclick(view v) {         switch (v.getid())         {             case r.id.logoutbutton:                   userlocalstore.clearuserdata();                 userlocalstore.setuserloggedin(false);                 intent intent = new intent(getactivity(), loginscreen.class);                 startactivity(intent);                  break;         }     } } 

and here shared preferences class use ;

public class userlocalstore {     public static final string sp_name = "userdetails";     sharedpreferences userlocaldatabase;      public userlocalstore(loginscreen context)     {         userlocaldatabase = context.getsharedpreferences(sp_name,0);     }     public void storeuserdata(user user)     {         sharedpreferences.editor speditor = userlocaldatabase.edit();         speditor.putstring("username",user.username);         speditor.putstring("password",user.password);         speditor.putstring("email",user.email);         speditor.putstring("phonenumber",user.phonenumber);         speditor.putstring("bloodtype",user.bloodtype);         speditor.putstring("birthday",user.birthday);         speditor.putstring("address",user.address);         speditor.commit();     }      public user getloggedinuser()     {         string username = userlocaldatabase.getstring("username","");         string password = userlocaldatabase.getstring("password","");         string email = userlocaldatabase.getstring("email","");         string phonenumber = userlocaldatabase.getstring("phonenumber","");         string bloodtype = userlocaldatabase.getstring("bloodtype","");         string birthday = userlocaldatabase.getstring("birthday","");         string address = userlocaldatabase.getstring("address","");          user storeduser = new user(username,password,email,phonenumber,bloodtype,birthday,address);         return storeduser;     }      public void setuserloggedin(boolean loggedin)     {         sharedpreferences.editor speditor = userlocaldatabase.edit();         speditor.putboolean("loggedin",loggedin);         speditor.commit();     }      public boolean getuserloogedin()     {         if (userlocaldatabase.getboolean("loggedin",false)!= true)         {             return true;         }         else         {             return false;         }      }      public void clearuserdata()     {         sharedpreferences.editor speditor = userlocaldatabase.edit();         speditor.clear();         speditor.commit();      } } 

so still think problem how call method, correct way of doing ?

edit: here error section of logcat ;

04-29 20:41:37.272  31883-31883/com.okanyakit.watchme e/imgsrv﹕ :0: pvrdrmopen: tp3, ret = 46 04-29 20:41:37.272  31883-31883/com.okanyakit.watchme e/imgsrv﹕ :0: pvrdrmopen: tp3, ret = 49 04-29 20:41:37.272  31883-31883/com.okanyakit.watchme e/imgsrv﹕ :0: pvrdrmopen: tp3, ret = 50 04-29 20:41:37.272  31883-31883/com.okanyakit.watchme e/imgsrv﹕ :0: pvrdrmopen: tp3, ret = 50 04-29 20:41:37.272  31883-31883/com.okanyakit.watchme e/imgsrv﹕ :0: pvrdrmopen: tp3, ret = 50 04-29 20:41:37.282  31883-31883/com.okanyakit.watchme e/imgsrv﹕ :0: pvrdrmopen: tp3, ret = 52 04-29 20:43:36.752  31883-31883/com.okanyakit.watchme e/androidruntime﹕ fatal exception: main     process: com.okanyakit.watchme, pid: 31883     java.lang.nullpointerexception             @ com.okanyakit.watchme.userprofile.authenticate(userprofile.java:69)             @ com.okanyakit.watchme.userprofile.onstart(userprofile.java:48)             @ android.support.v4.app.fragment.performstart(fragment.java:1810)             @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:987)             @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1136)             @ android.support.v4.app.backstackrecord.run(backstackrecord.java:739)             @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1499)             @ android.support.v4.app.fragmentmanagerimpl$1.run(fragmentmanager.java:456)             @ android.os.handler.handlecallback(handler.java:733)             @ android.os.handler.dispatchmessage(handler.java:95)             @ android.os.looper.loop(looper.java:149)             @ android.app.activitythread.main(activitythread.java:5257)             @ java.lang.reflect.method.invokenative(native method)             @ java.lang.reflect.method.invoke(method.java:515)             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793)             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:609)             @ dalvik.system.nativestart.main(native method) 04-29 20:48:37.142  32326-32326/com.okanyakit.watchme e/imgsrv﹕ :0: pvrdrmopen: tp3, ret = 46 04-29 20:48:37.152  32326-32326/com.okanyakit.watchme e/imgsrv﹕ :0: pvrdrmopen: tp3, ret = 49 04-29 20:48:37.152  32326-32326/com.okanyakit.watchme e/imgsrv﹕ :0: pvrdrmopen: tp3, ret = 50 04-29 20:48:37.152  32326-32326/com.okanyakit.watchme e/imgsrv﹕ :0: pvrdrmopen: tp3, ret = 50 04-29 20:48:37.152  32326-32326/com.okanyakit.watchme e/imgsrv﹕ :0: pvrdrmopen: tp3, ret = 50 04-29 20:48:37.152  32326-32326/com.okanyakit.watchme e/imgsrv﹕ :0: pvrdrmopen: tp3, ret = 52 

in method should say:

userlocalstore userlocalstore = new userlocalstore(); if(userlocalstore.getloggedinuser()){     //logged in  }else{     intent intent = new intent(getactivity(), loginscreen.class);     startactivityforresult(intent, 1); } 

and override method onactivityresult

@override protected void onactivityresult(int requestcode, int resultcode, intent data) {     if (requestcode == 1) {          boolean isloggedin = data.getbooleanextra("isloggedin");     } } 

edit

and when user submit input info clicking button check, save , other stuff want, if okay, run code:

@override   public void onclick(view v) {     // want      intent intent = new intent();      intent.putextra("isloggedin", mymethodtogetinfoisloggedin);      setresult(result_ok, intent);      finish();   } 

this proper way check if user logged in , if not - send him log in. hope helps.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -