samedi 20 février 2016

Using a session variable between classes after login - null object reference

I have created an app where you can login using SQLite and it checks the database for matching users.

I have got the login working and I've got it to set a boolean variable to true once the user has logged in, but I am trying to figure out how to pass it back to MainActivity.java from Login.java in the onResume() method.

Here is the code from Login.java, I set the session to true then I use putExtra to send it back to MainActivity.

public void onClick(View v) {
                String username = usernameET.getText().toString();
                String password = passwordET.getText().toString();  
                boolean success = db.Login(username, password);       

                if(success)
                {
                    Log.d("login", "user logged");                  
                    session = true;                     
                    Intent i = new Intent(Login.this, MainActivity.class);
                    i.putExtra("loginSuccess", session);
                    i.putExtra("sessionName", username);
                    startActivity(i);

                }
                else
                {
                    Log.d("login", "user not logged");
                }
            }

And here is the code from MainActivity.java in the onResume() method. The if statement is where the error is, I just want to know how I can get my application to recognise the boolean so it knows the user is logged in when returning the the main page, and stores the users name so that I can then load the user's information depending on who is logged in.

protected void onResume() {
    super.onRestart();
    Log.d("On Restart", "ON RESTART CALLED");
    boolean sessionBool = false;
    if (sessionBool = getIntent().getExtras().getBoolean("loginSuccess"))
    {
        sessionBool = true;
        String sessionNamestring = getIntent().getStringExtra("sessionName");
        Log.d("Session Name", sessionNamestring);

    }
    else
    {
        sessionBool = false;
    }

Logcat

02-21 00:54:29.174: E/AndroidRuntime(2563): FATAL EXCEPTION: main
02-21 00:54:29.174: E/AndroidRuntime(2563): Process: com.example.project, PID: 2563
02-21 00:54:29.174: E/AndroidRuntime(2563): java.lang.RuntimeException: Unable to resume activity {com.example.project/com.example.project.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Bundle.getBoolean(java.lang.String)' on a null object reference
02-21 00:54:29.174: E/AndroidRuntime(2563):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2989)
02-21 00:54:29.174: E/AndroidRuntime(2563):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3020)
02-21 00:54:29.174: E/AndroidRuntime(2563):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
02-21 00:54:29.174: E/AndroidRuntime(2563):     at android.app.ActivityThread.access$800(ActivityThread.java:151)
02-21 00:54:29.174: E/AndroidRuntime(2563):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
02-21 00:54:29.174: E/AndroidRuntime(2563):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-21 00:54:29.174: E/AndroidRuntime(2563):     at android.os.Looper.loop(Looper.java:135)
02-21 00:54:29.174: E/AndroidRuntime(2563):     at android.app.ActivityThread.main(ActivityThread.java:5257)
02-21 00:54:29.174: E/AndroidRuntime(2563):     at java.lang.reflect.Method.invoke(Native Method)
02-21 00:54:29.174: E/AndroidRuntime(2563):     at java.lang.reflect.Method.invoke(Method.java:372)
02-21 00:54:29.174: E/AndroidRuntime(2563):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
02-21 00:54:29.174: E/AndroidRuntime(2563):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
02-21 00:54:29.174: E/AndroidRuntime(2563): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Bundle.getBoolean(java.lang.String)' on a null object reference
02-21 00:54:29.174: E/AndroidRuntime(2563):     at com.example.project.MainActivity.onResume(MainActivity.java:64)
02-21 00:54:29.174: E/AndroidRuntime(2563):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257)
02-21 00:54:29.174: E/AndroidRuntime(2563):     at android.app.Activity.performResume(Activity.java:6076)

Aucun commentaire:

Enregistrer un commentaire