1

I am making a UDP server which receives a string, I have to modify that string, but I can't seem to figure out how to forward the string to the class that is supposed to handle the string, I get the following exception:

"can't create handler inside thread that has not called looper.prepare()"

I have little experience with android and can't figure out how to solve the problem, and none of the other questions about this seemed to give me the answer.

public class Server implements Runnable {

 @Override
 public void run() {
     while(!start)
     {
         //do nothing
     }
     try {
         InetAddress serverAddr = InetAddress.getByName(SERVERIP);
         //updateTrack("\nServer: Start connecting \n");
         DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddr);
         byte[] serverBuf = new byte[1064];
         DatagramPacket packet = new DatagramPacket(serverBuf, serverBuf.length);
         //updateTrack("Server: Receiving \n");
         socket.receive(packet);
         int realSize = packet.getLength();
         byte[] RealServerBuf = new byte[realSize];
         System.arraycopy (serverBuf, 0, RealServerBuf, 0, realSize);
         recived =  new String(RealServerBuf);
         //updateTrack("Server: " + recived + "\n");
         StringHandler stringHandler = new StringHandler();  
         stringHandler.StringSplitter(recived);
         updateTrack("Server: Succeed\n");
     } catch (Exception e) {
         updateTrack("Server: Error\n" + e);
     }
   }
}

Tthe problem has something to do with how I instantiate the StringHandler.class and it not being connected with the UI-thread, but I can't figure out how to do it.

Narendra Singh
  • 3,990
  • 5
  • 37
  • 78
Mark G
  • 39
  • 5
  • 2
    "the problem has something to do with how I instantiate the StringHandler.class" -- Android does not have a `StringHandler` class. We cannot help you with it, if we do not know what it is. Beyond that, please post the Java stack trace associated with that crash, pointing out what lines in the stack trace correspond to lines in your code snippets in your question. – CommonsWare May 14 '15 at 12:23
  • Welcome to stack overflow! To get the best help, you need to tell us what `StringHandler` is (e.g. a link to the API documentation), put the full stack trace in the question and clearly identify where the exception is being thrown and its relation to where (if?) you call `looper.prepare()` that is referenced in the stack trace. This question might also help you http://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare it might even be a duplicate, but without full info we can't be sure. I got that from searching "looper.prepare() android" – J Richard Snape May 14 '15 at 12:30
  • @MarkG check the answer I posted. May be helpful. – Narendra Singh May 14 '15 at 13:43

2 Answers2

0

Put your code inside runOnUiThread

activity.runOnUiThread(new Runnable() {
  public void run() {
    // your code here
  }
});

Please, look at this for further details.

Community
  • 1
  • 1
Narendra Singh
  • 3,990
  • 5
  • 37
  • 78
0

Put Your code inside

activity.runOnUiThread(new Runnable() {
  public void run() {
   // your code here
 }
});
Narendra Singh
  • 3,990
  • 5
  • 37
  • 78