2012-02-17 50 views
0

遇到錯誤創建處理程序...不能()內螺紋已不叫Looper.prepare創建處理程序了java.lang.RuntimeException:無法內螺紋

其實我的情況是這樣的: 我正嘗試使用Android開發人員提供的「BluetoothChat」示例代碼。我的任務是設置一個應用程序運行並在遠程設備連接後自動發送消息...我想你們都知道我想說什麼... 我有字符串消息,我希望應用程序每隔一秒發送一次設備:

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

在這裏,我試圖改變在那裏我覺得一個應用程序會做我想做的與失敗的碼的某些部分... :(

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); 

    } 

回答

相關問題