Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- loginconfig = new AppConfig();
- txtHeader = (TextView) findViewById(R.id.login_header);
- editEmail = (EditText) findViewById(R.id.loginEmail);
- editPassword = (EditText) findViewById(R.id.loginPassword);
- txtSignUp = (TextView) findViewById(R.id.txtSign);
- btnLogin = (Button) findViewById(R.id.btnLogin);
- sp_session = this.getSharedPreferences("Session_User", MODE_PRIVATE);
- editor_session = sp_session.edit();
- if (sessionManager.isLoggedIn()) {
- String id = sp_session.getString("user_id", null);
- Intent intent = new Intent(getApplicationContext(), MainActivity.class);
- intent.putExtra("user_id", id);
- startActivity(intent);
- finish();
- }
- btnLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(getApplicationContext(), MainActivity.class);
- startActivity(intent);
- }
- });
- txtSignUp.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (loginconfig.validateBlank(getApplicationContext(), editEmail, "Email Address")) {
- if (loginconfig.validateBlank(getApplicationContext(), editPassword, "Password")) {
- new send_Login_Info().execute();
- }
- }
- }
- });
- }
- public class send_Login_Info extends AsyncTask<String, String, JSONObject> {
- String lemail = editEmail.getText().toString();
- String lpassword = editPassword.getText().toString();
- JSONParser jsonParser = new JSONParser();
- private static final String LOGIN_URL = "http://laundry.cattpre.com/mob_app/index/user_login.php";
- private static final String TAG_MESSAGE = "result";
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- pDialog.show();
- }
- @Override
- protected JSONObject doInBackground(String... args) {
- try {
- HashMap<String, String> params = new HashMap<>();
- params.put("name", lemail);
- params.put("password", lpassword);
- Log.d("request", "starting");
- JSONObject json = jsonParser.makeHttpRequest(
- LOGIN_URL, "POST", params);
- if (json != null) {
- Log.d("JSON result", json.toString());
- return json;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- protected void onPostExecute(JSONObject json) {
- if (pDialog != null && pDialog.isShowing()) {
- pDialog.dismiss();
- }
- if (json != null) {
- /* Toast.makeText(SignUp.this, json.toString(),
- Toast.LENGTH_LONG).show();
- */
- try {
- // result = json.getInt(TAG_SUCCESS);
- result = json.getString(TAG_MESSAGE);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- if (result.equals("true")) {
- sessionManager.createLoginSession(editEmail.getText().toString(), editPassword.getText().toString());
- Toast.makeText(getApplicationContext(), "Login Successfull", Toast.LENGTH_SHORT).show();
- try {
- userinfo = json.getJSONArray("users_info");
- for (int i = 0; i < userinfo.length(); i++) {
- JSONObject c = userinfo.optJSONObject(i);
- if (c != null) {
- user_id = c.getString("users_id");
- }
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- editor_session.putString("user_id", user_id);
- editor_session.commit();
- Intent i = new Intent(LoginActivity.this, MainActivity.class);
- i.putExtra("user_id", user_id);
- startActivity(i);
- finish();
- } else {
- Toast.makeText(getApplicationContext(), "Invalid Deatils... Try Again", Toast.LENGTH_SHORT).show();
- }
- }
- }
- }
- public class SessionManager
- {
- SharedPreferences pref;
- // Editor for Shared preferences
- SharedPreferences.Editor editor;
- // Context
- Context _context;
- // Shared pref mode
- int PRIVATE_MODE = 0;
- private static final String PREF_NAME = "Laundry";
- // All Shared Preferences Keys
- private static final String IS_LOGIN = "IsLoggedIn";
- // User name (make variable public to access from outside)
- public static final String KEY_NAME = "name";
- // Email address (make variable public to access from outside)
- public static final String KEY_PASS = "password";
- // Constructor
- public SessionManager(Context context){
- this._context = context;
- pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
- editor = pref.edit();
- }
- /**
- * Create login session
- * */
- public void createLoginSession(String name, String password){
- // Storing login value as TRUE
- editor.putBoolean(IS_LOGIN, true);
- // Storing name in pref
- editor.putString(KEY_NAME, name);
- // Storing email in pref
- editor.putString(KEY_PASS, password);
- // commit changes
- editor.commit();
- }
- /**
- * Check login method wil check user login status
- * If false it will redirect user to login page
- * Else won't do anything
- * */
- public void checkLogin(){
- // Check login status
- if(!this.isLoggedIn()){
- // user is not logged in redirect him to Login Activity
- Intent i = new Intent(_context, LoginActivity.class);
- // Closing all the Activities
- i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- // Add new Flag to start new Activity
- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- // Staring Login Activity
- _context.startActivity(i);
- }
- }
- /**
- * Get stored session data
- * */
- public HashMap<String, String> getUserDetails(){
- HashMap<String, String> user = new HashMap<String, String>();
- // user name
- user.put(KEY_NAME, pref.getString(KEY_NAME, null));
- // user email id
- user.put(KEY_PASS, pref.getString(KEY_PASS, null));
- // return user
- return user;
- }
- /**
- * Clear session details
- * */
- public void logoutUser(){
- // Clearing all data from Shared Preferences
- editor.clear();
- editor.commit();
- // After logout redirect user to Loing Activity
- Intent i = new Intent(_context, LoginActivity.class);
- // Closing all the Activities
- i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- // Add new Flag to start new Activity
- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- // Staring Login Activity
- _context.startActivity(i);
- }
- // Get Login State
- public boolean isLoggedIn(){
- return pref.getBoolean(IS_LOGIN, false);
- }
- }
- 05-10 06:25:29.479 17037-17037/com.projects.cattpre.laundry E/AndroidRuntime: FATAL EXCEPTION: main
- Process: com.projects.cattpre.laundry, PID: 17037
- 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
- at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
- at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
- at android.app.ActivityThread.access$800(ActivityThread.java:151)
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
- at android.os.Handler.dispatchMessage(Handler.java:102)
- at android.os.Looper.loop(Looper.java:135)
- at android.app.ActivityThread.main(ActivityThread.java:5254)
- at java.lang.reflect.Method.invoke(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:372)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
- Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.projects.cattpre.laundry.SessionManager.isLoggedIn()' on a null object reference
- at com.projects.cattpre.laundry.LoginActivity.onCreate(LoginActivity.java:53)
- at android.app.Activity.performCreate(Activity.java:5990)
- at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
- at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
- at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
- at android.app.ActivityThread.access$800(ActivityThread.java:151)
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
- at android.os.Handler.dispatchMessage(Handler.java:102)
- at android.os.Looper.loop(Looper.java:135)
- at android.app.ActivityThread.main(ActivityThread.java:5254)
- at java.lang.reflect.Method.invoke(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:372)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
- 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