Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Service-Activity 2 way communication
    primarykey
    data
    text
    <p>in my team's Android application I have a service running from boot which communicates with a server to perform operations such as logging in, registering, chatting between phones and updating the phone database.</p> <p>I need to make my service communicate with the activity bi-directionally: for example I am working on the login activity at the moment and the username and passwords are Strings taken from a text field on the app screen and I have been able to pass them to the service for it to send an authorisation command to the server.</p> <pre><code>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) { } </code></pre> <p>This works as I would have expected. When the server responds with a message saying whether or not the login was sucessful, I need it to pass a message back to the activity so that I can start the main activity if succesful or prompt for re-entry if not.</p> <p>I tried to use the msg.replyTo field to get the return messenger to send the information back, but when I run the app it force closes with a null pointer exception and I have no idea why this is happening. Here is the code that seems to be the culprit:</p> <pre><code>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; </code></pre> <p>The null pointer seems to be coming from the </p> <pre><code>msg.replyTo.send(m); </code></pre> <p>line of code in both cases (login succesful and login failed)</p> <p>Any help to fix this problem would be greatly appreciated :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload