0

Here is my code:

public void button_sendMessage(){
  new SendPhoneTask().execute();
}

public class SendPhoneTask extends AsyncTask<Void, Void, Void> {
  @Override
  protected Void doInBackground(Void... args) {
    Collection<String> nodes = getNodes();
    for (String node : nodes) {
      sendDataMessage(node);
    }
    return null;
  }
}

private Collection<String> getNodes() {
  HashSet<String> results = new HashSet<>();
  NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi
                                         .getConnectedNodes(mGoogleApiClient).await();

  for (Node node : nodes.getNodes()) {
    results.add(node.getId());
  }

  return results;
}

I'm trying to send a message from my phone to my android wear device. But error occurs.
What is wrong with my code?

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
지영채
  • 11
  • 4
  • what is your error or do i need to go through all to find it for you? no gods powers here – Charuක Jul 23 '16 at 02:57
  • it is a FATAL EXCEPTION: AsyncTask #1 Process: com.example.jameschee.babystat, PID: 20595 java.lang.RuntimeException: An error occured while executing doInBackground() – 지영채 Jul 23 '16 at 03:03
  • andCaused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() – 지영채 Jul 23 '16 at 03:04
  • where is ur sendDataMessage(); method ? May be you are trying to change something in main UI using doInBackground,which you cannot do , full class might useful to solve the issue – Charuක Jul 23 '16 at 03:09
  • private void sendDataMessage(String node) { Context context = getApplicationContext(); Toast succeed = Toast.makeText(context,"succeed2",Toast.LENGTH_SHORT); succeed.show(); – 지영채 Jul 23 '16 at 03:15
  • Wearable.MessageApi.sendMessage( mGoogleApiClient, node, SEND_DATA, new byte[0]).setResultCallback( new ResultCallback() { @Override public void onResult(MessageApi.SendMessageResult sendMessageResult) { if (!sendMessageResult.getStatus().isSuccess()) { – 지영채 Jul 23 '16 at 03:16
  • Log.e(TAG, "Failed to send message with status code: " + sendMessageResult.getStatus().getStatusCode()); Context context = getApplicationContext(); Toast succeed = Toast.makeText(context,"crashed",Toast.LENGTH_SHORT); succeed.show(); – 지영채 Jul 23 '16 at 03:16
  • this is my send message method – 지영채 Jul 23 '16 at 03:16
  • Welcome to Stack Overflow! I edited your question as far as I could guess your problem. However, add error and description so that more people with knowledge of the subject will see it. Please edit in the specific error-message you're encountering in case that's necessary to identify the specific problem. Good Luck! – Enamul Hassan Jul 23 '16 at 04:54

1 Answers1

1

Move the toast inside

If fragment then use

getActivity().runOnUiThread(new Runnable (

  // toast

If Activity then use

runOnUiThread(new Runnable 

  // toast

Hope this helps

Arunjith R S
  • 792
  • 12
  • 23
Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49