0

I am using Androids SaveSharedPreference to allow a user to auto login once they already logged in previously. For a user to gain access, their login information must be sent to my server using webscokets, and once a connection is made they will be able to access their accounts.

In terms of present functionality, the users can log into the app the first time and use all the features. The problem arises as the user closes out, not logs out, and attempts to access again and the error below is thrown and the app crashes:

enter image description here

and then

enter image description here

I have seen that the NullPoint error on line 207 of LoggingIn is at:

result = imService.authenticateUser(
                                SaveSharedPreference
                                        .getUserName(getApplicationContext()),
                                SaveSharedPreference
                                        .getPassword(getApplicationContext()));

The remaining code of the class in the onCreate method of the LoggingIn class called once app starts:

protected static final int NOT_CONNECTED_TO_SERVICE = 0;
    protected static final int FILL_BOTH_USERNAME_AND_PASSWORD = 1;
    public static final String AUTHENTICATION_FAILED = "0";
    public static final String FRIEND_LIST = "FRIEND_LIST";
    protected static final int MAKE_SURE_USERNAME_AND_PASSWORD_CORRECT = 2;
    protected static final int NOT_CONNECTED_TO_NETWORK = 3;
    private EditText usernameText;
    private EditText passwordText;

    private Manager imService;
    public static final int SIGN_UP_ID = Menu.FIRST;
    public static final int EXIT_APP_ID = Menu.FIRST + 1;

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            // This is called when the connection with the service has been
            // established, giving us the service object we can use to
            // interact with the service. Because we have bound to a explicit
            // service that we know is running in our own process, we can
            // cast its IBinder to a concrete class and directly access it.
            imService = ((MessagingService.IMBinder) service).getService();

            if (imService.isUserAuthenticated() == true) {
                // Intent i = new Intent(LoggingIn.this, ListOfFriends.class);
                Intent i = new Intent(LoggingIn.this, MainActivity.class);
                startActivity(i);
                LoggingIn.this.finish();
            }
        }

        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            // Because it is running in our same process, we should never
            // see this happen.
            imService = null;
            Toast.makeText(LoggingIn.this, R.string.local_service_stopped,
                    Toast.LENGTH_SHORT).show();
        }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*
         * Start and bind the imService
         */
        startService(new Intent(LoggingIn.this, MessagingService.class));

        setContentView(R.layout.loggin_in);
        setTitle("Login");

        ImageButton loginButton = (ImageButton) findViewById(R.id.button1);

        usernameText = (EditText) findViewById(R.id.username);
        passwordText = (EditText) findViewById(R.id.password);

        // If not logged in already
        if (SaveSharedPreference.getUserName(getApplicationContext()).length() == 0) {

            loginButton.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    if (imService == null) {
                        Toast.makeText(getApplicationContext(),
                                R.string.not_connected_to_service,
                                Toast.LENGTH_LONG).show();
                        // showDialog(NOT_CONNECTED_TO_SERVICE);
                        return;
                    } else if (imService.isNetworkConnected() == false) {
                        Toast.makeText(getApplicationContext(),
                                R.string.not_connected_to_network,
                                Toast.LENGTH_LONG).show();
                        // showDialog(NOT_CONNECTED_TO_NETWORK);

                    } else if (usernameText.length() > 0
                            && passwordText.length() > 0) {

                        Thread loginThread = new Thread() {
                            private Handler handler = new Handler();

                            @Override
                            public void run() {
                                String result = null;

                                try {
                                    result = imService.authenticateUser(
                                            usernameText.getText().toString(),
                                            passwordText.getText().toString());
                                } catch (UnsupportedEncodingException e) {

                                    e.printStackTrace();
                                }
                                if (result == null
                                        || result.equals(AUTHENTICATION_FAILED)) {
                                    /*
                                     * Authenticatin failed, inform the user
                                     */
                                    handler.post(new Runnable() {
                                        public void run() {
                                            Toast.makeText(
                                                    getApplicationContext(),
                                                    R.string.make_sure_username_and_password_correct,
                                                    Toast.LENGTH_LONG).show();

                                            // showDialog(MAKE_SURE_USERNAME_AND_PASSWORD_CORRECT);
                                        }
                                    });

                                } else {

                                    /*
                                     * if result not equal to authentication
                                     * failed, result is equal to friend and
                                     * group list of the user 0: is for friends,
                                     * 1: is for groups
                                     */
                                    handler.post(new Runnable() {
                                        public void run() {

                                            // If log in successful, then save
                                            // username and password to shared
                                            // preferences:

                                            SaveSharedPreference.setUserName(
                                                    getApplicationContext(),
                                                    usernameText.getText()
                                                            .toString());

                                            SaveSharedPreference.setPassword(
                                                    getApplicationContext(),
                                                    passwordText.getText()
                                                            .toString());

                                            Intent i = new Intent(
                                                    LoggingIn.this,
                                                    MainActivity.class);
                                            startActivity(i);
                                            LoggingIn.this.finish();

                                        }
                                    });

                                }

                            }
                        };
                        loginThread.start();

                    } else {
                        /*
                         * Username or Password is not filled, alert the user
                         */
                        Toast.makeText(getApplicationContext(),
                                R.string.fill_both_username_and_password,
                                Toast.LENGTH_LONG).show();
                        // showDialog(FILL_BOTH_USERNAME_AND_PASSWORD);
                    }
                }
            });

        } else {

            // If already logged in, pull the information and send to server to
            // auto log in

            Thread loginThread = new Thread() {
                private Handler handler = new Handler();

                @Override
                public void run() {
                    String result = null;

                    try {
                        result = imService.authenticateUser(
                                SaveSharedPreference
                                        .getUserName(getApplicationContext()),
                                SaveSharedPreference
                                        .getPassword(getApplicationContext()));
                    } catch (UnsupportedEncodingException e) {

                        e.printStackTrace();
                    }

                    if (result == null || result.equals(AUTHENTICATION_FAILED)) {
                        /*
                         * Authenticatin failed, inform the user
                         */
                        handler.post(new Runnable() {
                            public void run() {
                                Toast.makeText(
                                        getApplicationContext(),
                                        R.string.make_sure_username_and_password_correct,
                                        Toast.LENGTH_LONG).show();

                                // showDialog(MAKE_SURE_USERNAME_AND_PASSWORD_CORRECT);
                            }
                        });

                    } else {

                        /*
                         * if result not equal to authentication failed, result
                         * is equal to friend and group list of the user 0: is
                         * for friends, 1: is for groups
                         */
                        handler.post(new Runnable() {
                            public void run() {
                                Intent i = new Intent(LoggingIn.this,
                                        MainActivity.class);
                                startActivity(i);
                                LoggingIn.this.finish();

                            }
                        });

                    }

                }
            };
            loginThread.start();

        }

    }

How can I allow the users to automatically login with my service successfully?

Sauron
  • 6,399
  • 14
  • 71
  • 136
  • Your first exception appears to be a Samsung Android bug: http://stackoverflow.com/questions/23637835/pause-gc-error-post-android-4-4-2-upgrade – EJK Dec 09 '14 at 03:27
  • Your 2nd exception (line 207) probably means that "inservice" is null. Did you check this? – EJK Dec 09 '14 at 03:28
  • How can I check this? The service is started right in the onCreate at startService(new Intent(LoggingIn.this, MessagingService.class)); Does it need more time to boot up? Should I use sleep? – Sauron Dec 09 '14 at 03:47
  • First you need to verify that inservice is indeed null. That is my hypothesis, but it needs to be verified. Run this in the debugger and set a breakpoint at line 207. Inspect inservice to see if it is null. – EJK Dec 09 '14 at 03:49
  • It wont even get to that point, the second it opens it crashes – Sauron Dec 09 '14 at 04:02
  • Did I set up the `if` statesments correctly for a user to be able to auto log in, I feel having the button inside an if statement might not be the best design – Sauron Dec 09 '14 at 04:04
  • RE "It wont even get to that point, the second it opens it crashes". I don't understand. Your post shows a LogCat message indicating a NullPointerException on line 207. – EJK Dec 09 '14 at 04:12
  • I think a major problem is that the service hasn't had time to boot up by the time the app wants to launch an intent after LoggingIn and start MainActivity. How can I pause other actions until the service has successfully started? – Sauron Dec 09 '14 at 04:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66432/discussion-between-ejk-and-sauron). – EJK Dec 09 '14 at 04:38
  • I am very sorry, it is 11:45 pm here (eastern time), can we chat at 6pm (eastern time) tomorrow? – Sauron Dec 09 '14 at 04:42
  • Not sure if I will be online then. See my conversation comments. BTW, I am on eastern time too. – EJK Dec 09 '14 at 04:44
  • @EJK what time will work for you? – Sauron Dec 09 '14 at 16:28

0 Answers0