0

I'm getting errors...Can't create handler inside thread that has not called Looper.prepare()

Actually my case is like this: I'm trying to use the example code of "BluetoothChat" that I took from Android developer. My mission is set an app running and sending messages automatically after the remote device is connected... I think you all know what I'm trying to say... I have String messages that I want the app send every second for remote device:

String helloString[] = {"hello person"," hi there", "hola hola", "yau yau..."};

Here I tried to change some part of the code where i think an app will do what I want with failed... :(

private void setupChat() 
    {
    Log.d(TAG, "setupChat()");

    Thread output = new Thread()
    {
        public void run()
    {
            while (true)
            {
                for(int i = 0; i < 4; i++)
                {

                    //Sending helloStrings for the device
                    message = helloString[i];       
                    sendMessage(message);

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {

                    }
                }
            }
    }
    };
    output.start();

    // Initialize the BluetoothChatService to perform bluetooth connections
    mChatService = new BluetoothChatService(this, mHandler);


}

/**
     * Sends a message.
     * @param message  A string of text to send.
     */   
    private void sendMessage(String message) 
    {
        // TODO Auto-generated method stub
        // Check that we're actually connected before trying anything
        if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) 
        {
            Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();           
            return;
        }

        byte[] send = message.getBytes();
        mChatService.write(send);

    }
A.H.
  • 63,967
  • 15
  • 92
  • 126
foni vo
  • 1
  • 1

1 Answers1

0

You need to declare @Override on run() method too.

See the accepted answer to this question How to run a Runnable thread in Android?.

Community
  • 1
  • 1
RobGThai
  • 5,937
  • 8
  • 41
  • 59