Advertisement
Guest User

Untitled

a guest
May 10th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.07 KB | None | 0 0
  1. @Override
  2. protected void onCreate(@Nullable Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_login);
  5. loginconfig = new AppConfig();
  6.  
  7. txtHeader = (TextView) findViewById(R.id.login_header);
  8. editEmail = (EditText) findViewById(R.id.loginEmail);
  9. editPassword = (EditText) findViewById(R.id.loginPassword);
  10. txtSignUp = (TextView) findViewById(R.id.txtSign);
  11. btnLogin = (Button) findViewById(R.id.btnLogin);
  12.  
  13. sp_session = this.getSharedPreferences("Session_User", MODE_PRIVATE);
  14.  
  15. editor_session = sp_session.edit();
  16.  
  17. if (sessionManager.isLoggedIn()) {
  18.  
  19. String id = sp_session.getString("user_id", null);
  20.  
  21. Intent intent = new Intent(getApplicationContext(), MainActivity.class);
  22. intent.putExtra("user_id", id);
  23. startActivity(intent);
  24. finish();
  25. }
  26.  
  27. btnLogin.setOnClickListener(new View.OnClickListener() {
  28. @Override
  29. public void onClick(View v) {
  30. Intent intent = new Intent(getApplicationContext(), MainActivity.class);
  31. startActivity(intent);
  32. }
  33. });
  34.  
  35.  
  36. txtSignUp.setOnClickListener(new View.OnClickListener() {
  37. @Override
  38. public void onClick(View v) {
  39.  
  40. if (loginconfig.validateBlank(getApplicationContext(), editEmail, "Email Address")) {
  41. if (loginconfig.validateBlank(getApplicationContext(), editPassword, "Password")) {
  42.  
  43. new send_Login_Info().execute();
  44. }
  45. }
  46.  
  47. }
  48. });
  49. }
  50.  
  51.  
  52. public class send_Login_Info extends AsyncTask<String, String, JSONObject> {
  53.  
  54. String lemail = editEmail.getText().toString();
  55. String lpassword = editPassword.getText().toString();
  56.  
  57. JSONParser jsonParser = new JSONParser();
  58.  
  59. private static final String LOGIN_URL = "http://laundry.cattpre.com/mob_app/index/user_login.php";
  60.  
  61. private static final String TAG_MESSAGE = "result";
  62.  
  63.  
  64. @Override
  65. protected void onPreExecute() {
  66. super.onPreExecute();
  67.  
  68. pDialog.show();
  69. }
  70.  
  71. @Override
  72. protected JSONObject doInBackground(String... args) {
  73. try {
  74.  
  75. HashMap<String, String> params = new HashMap<>();
  76.  
  77. params.put("name", lemail);
  78. params.put("password", lpassword);
  79.  
  80. Log.d("request", "starting");
  81.  
  82. JSONObject json = jsonParser.makeHttpRequest(
  83. LOGIN_URL, "POST", params);
  84.  
  85. if (json != null) {
  86. Log.d("JSON result", json.toString());
  87.  
  88. return json;
  89. }
  90.  
  91. } catch (Exception e) {
  92. e.printStackTrace();
  93. }
  94. return null;
  95. }
  96.  
  97.  
  98. protected void onPostExecute(JSONObject json) {
  99.  
  100. if (pDialog != null && pDialog.isShowing()) {
  101. pDialog.dismiss();
  102. }
  103.  
  104. if (json != null) {
  105. /* Toast.makeText(SignUp.this, json.toString(),
  106. Toast.LENGTH_LONG).show();
  107. */
  108. try {
  109. // result = json.getInt(TAG_SUCCESS);
  110. result = json.getString(TAG_MESSAGE);
  111. } catch (JSONException e) {
  112. e.printStackTrace();
  113. }
  114. }
  115.  
  116. if (result.equals("true")) {
  117. sessionManager.createLoginSession(editEmail.getText().toString(), editPassword.getText().toString());
  118. Toast.makeText(getApplicationContext(), "Login Successfull", Toast.LENGTH_SHORT).show();
  119.  
  120. try {
  121. userinfo = json.getJSONArray("users_info");
  122.  
  123.  
  124. for (int i = 0; i < userinfo.length(); i++) {
  125. JSONObject c = userinfo.optJSONObject(i);
  126. if (c != null) {
  127. user_id = c.getString("users_id");
  128.  
  129. }
  130.  
  131. }
  132. } catch (JSONException e) {
  133. e.printStackTrace();
  134. }
  135. editor_session.putString("user_id", user_id);
  136.  
  137. editor_session.commit();
  138. Intent i = new Intent(LoginActivity.this, MainActivity.class);
  139. i.putExtra("user_id", user_id);
  140. startActivity(i);
  141. finish();
  142.  
  143. } else {
  144. Toast.makeText(getApplicationContext(), "Invalid Deatils... Try Again", Toast.LENGTH_SHORT).show();
  145.  
  146. }
  147. }
  148.  
  149. }
  150.  
  151. }
  152.  
  153. public class SessionManager
  154. {
  155. SharedPreferences pref;
  156.  
  157. // Editor for Shared preferences
  158. SharedPreferences.Editor editor;
  159.  
  160. // Context
  161. Context _context;
  162.  
  163. // Shared pref mode
  164. int PRIVATE_MODE = 0;
  165. private static final String PREF_NAME = "Laundry";
  166.  
  167. // All Shared Preferences Keys
  168. private static final String IS_LOGIN = "IsLoggedIn";
  169.  
  170. // User name (make variable public to access from outside)
  171. public static final String KEY_NAME = "name";
  172.  
  173. // Email address (make variable public to access from outside)
  174. public static final String KEY_PASS = "password";
  175.  
  176. // Constructor
  177. public SessionManager(Context context){
  178. this._context = context;
  179. pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
  180. editor = pref.edit();
  181. }
  182. /**
  183. * Create login session
  184. * */
  185. public void createLoginSession(String name, String password){
  186. // Storing login value as TRUE
  187. editor.putBoolean(IS_LOGIN, true);
  188.  
  189. // Storing name in pref
  190. editor.putString(KEY_NAME, name);
  191.  
  192. // Storing email in pref
  193. editor.putString(KEY_PASS, password);
  194.  
  195. // commit changes
  196. editor.commit();
  197. }
  198. /**
  199. * Check login method wil check user login status
  200. * If false it will redirect user to login page
  201. * Else won't do anything
  202. * */
  203. public void checkLogin(){
  204. // Check login status
  205. if(!this.isLoggedIn()){
  206. // user is not logged in redirect him to Login Activity
  207. Intent i = new Intent(_context, LoginActivity.class);
  208. // Closing all the Activities
  209. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  210.  
  211. // Add new Flag to start new Activity
  212. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  213.  
  214. // Staring Login Activity
  215. _context.startActivity(i);
  216. }
  217.  
  218. }
  219. /**
  220. * Get stored session data
  221. * */
  222. public HashMap<String, String> getUserDetails(){
  223. HashMap<String, String> user = new HashMap<String, String>();
  224. // user name
  225. user.put(KEY_NAME, pref.getString(KEY_NAME, null));
  226.  
  227. // user email id
  228. user.put(KEY_PASS, pref.getString(KEY_PASS, null));
  229.  
  230. // return user
  231. return user;
  232. }
  233.  
  234. /**
  235. * Clear session details
  236. * */
  237. public void logoutUser(){
  238. // Clearing all data from Shared Preferences
  239. editor.clear();
  240. editor.commit();
  241.  
  242. // After logout redirect user to Loing Activity
  243. Intent i = new Intent(_context, LoginActivity.class);
  244. // Closing all the Activities
  245. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  246.  
  247. // Add new Flag to start new Activity
  248. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  249.  
  250. // Staring Login Activity
  251. _context.startActivity(i);
  252.  
  253. }
  254. // Get Login State
  255. public boolean isLoggedIn(){
  256. return pref.getBoolean(IS_LOGIN, false);
  257. }
  258. }
  259.  
  260. 05-10 06:25:29.479 17037-17037/com.projects.cattpre.laundry E/AndroidRuntime: FATAL EXCEPTION: main
  261. Process: com.projects.cattpre.laundry, PID: 17037
  262. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.projects.cattpre.laundry/com.projects.cattpre.laundry.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.projects.cattpre.laundry.SessionManager.isLoggedIn()' on a null object reference
  263. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
  264. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
  265. at android.app.ActivityThread.access$800(ActivityThread.java:151)
  266. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
  267. at android.os.Handler.dispatchMessage(Handler.java:102)
  268. at android.os.Looper.loop(Looper.java:135)
  269. at android.app.ActivityThread.main(ActivityThread.java:5254)
  270. at java.lang.reflect.Method.invoke(Native Method)
  271. at java.lang.reflect.Method.invoke(Method.java:372)
  272. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
  273. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  274. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.projects.cattpre.laundry.SessionManager.isLoggedIn()' on a null object reference
  275. at com.projects.cattpre.laundry.LoginActivity.onCreate(LoginActivity.java:53)
  276. at android.app.Activity.performCreate(Activity.java:5990)
  277. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
  278. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
  279. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
  280. at android.app.ActivityThread.access$800(ActivityThread.java:151) 
  281. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
  282. at android.os.Handler.dispatchMessage(Handler.java:102) 
  283. at android.os.Looper.loop(Looper.java:135) 
  284. at android.app.ActivityThread.main(ActivityThread.java:5254) 
  285. at java.lang.reflect.Method.invoke(Native Method) 
  286. at java.lang.reflect.Method.invoke(Method.java:372) 
  287. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
  288. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
  289. 05-10 06:30:29.814 17037-17037/com.projects.cattpre.laundry I/Process: Sending signal. PID: 17037 SIG: 9
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement