on my Android application team I have a boot-based service that interacts with the server to perform operations such as logging in, registering, talking between phones and updating the phone database.
I need my service to communicate with activity in two directions: for example, Iām currently working on the login reality, and the username and passwords are the lines taken from the text field on the application screen, and I could transfer them to the service so that it would send to server authorization command.
public void loginPressed(View v){ usernameStr = usernameField.getText().toString(); passwordStr = passwordField.getText().toString(); if (!bound) return; Bundle b = new Bundle(); Message msg = Message.obtain(null, ChatService.LOGIN); try { b.putString("username", usernameStr); b.putString("password", passwordStr); msg.setData(b); messenger.send(msg); } catch (RemoteException e) { }
It works as I expected. When the server responds with a message about whether the login was successful, I need it to pass the message back to the action so that I can start the main action if it was successful or suggested re-recording if not.
I tried to use the msg.replyTo field to return the returned messenger to send the information back, but when I launch the application, it forces me to close the null pointer exception, and I have no idea why this happens. Here is the code that seems to be the culprit:
private class IncomingHandler extends Handler { @Override public void handleMessage(Message msg) { switch(msg.what) { case LOGIN: Bundle b = msg.getData(); String username = b.getString("username"); String password = b.getString("password"); String loginMessage = TCPCall.login(username, password); connection.sendMessage(loginMessage); String loginReturn = connection.retrieveMessage(); Message m; Scanner s = new Scanner(loginReturn); s.useDelimiter(","); String c = s.next(); String status = s.next(); String message = s.next(); if (status.equals("OK")) { m = Message.obtain(null, LoginActivity.OK); try { msg.replyTo.send(m); } catch (RemoteException e) {} } else { m = Message.obtain(null, LoginActivity.ERR); try { msg.replyTo.send(m); } catch (RemoteException e) {} } break;
The null pointer seems to come from
msg.replyTo.send(m);
line of code in both cases (login completed successfully and login failed)
Any help to fix this problem would be greatly appreciated :)