2012-06-21 9 views
1

我正在編寫一個簡單的應用程序,用於在本地網絡上的服務器和應用程序之間進行通信。編寫和接收消息毫無問題。但是我無法顯示我在Textview中收到的消息。即使使用處理程序,也無法從另一個線程訪問視圖

對於接收傳入消息,我使用(工作)線程。當我收到一些東西時,我會將消息發送給mainthread中的一個處理程序,然後應該在Textview中顯示消息。
但是每次處理程序試圖顯示消息,我都會得到以下異常:
「只有創建視圖層次結構的原始線程才能觸及其視圖。」

我搜索了很多網站,包括這一個,找到一個解決方案,但仍然無法解決問題。請幫助我。

代碼:

public class TCPClientActivity extends Activity { 
    BufferedWriter output; 
    Socket s = new Socket(); 

    //my handler 
    private Handler mHandler; 
    //where I want to display the message 
    private TextView lblChat; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tcpclient); 

     Button btnConnect = (Button)findViewById(R.id.btnConnect); 
     Button btnSend = (Button) findViewById(R.id.btnSend); 
     //creating the TextView 
     lblChat = (TextView) findViewById(R.id.lblChat); 
     final EditText tbIP = (EditText) findViewById(R.id.tbIP); 
     final EditText tbInput = (EditText) findViewById(R.id.tbInput); 

     //creating the handler 
     mHandler = new Handler() 
     { 
      @Override 
      public void handleMessage(Message msg) 
      { 
       switch(msg.what) 
       { 
        case 1: 
        { 
         lblChat.append("Received: " + msg.obj + "\r\n"); 
        } 
       } 
      } 
     }; 
} 

//Thread in which I receive incoming Messages from the server 
    Thread communication = new Thread() 
    { 
     @Override 
     public void run() 
     { 
      String finalText = ""; 

      while(true) 
      { 
       try 
       { 
        DataInputStream inputStream = new DataInputStream(s.getInputStream()); 

        int bytesRead; 
        byte[] b = new byte[4096]; 

        bytesRead = inputStream.read(b,0,b.length); 

       finalText = EncodingUtils.getAsciiString(b); 

       //sending the message to the handler 
        Message msg = new Message(); 
        msg.what = 1; 
        msg.obj = finalText; 

        mHandler.handleMessage(msg); 
      } 

       //android.view.ViewRoot$CalledFromWrongThreadException: 
       //Only the original thread that created a view hierarchy can touch its views. 
       //That's the exception I get when running the program 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 
     } 
    }; 

//method where I connect to the server 
    private void connectToServer(String ip) 
    { 
     try 
     { 
      if(!ip.equals("")) 
      { 
       s = new Socket(ip, 3000); 
      } 

      else 
      { 
       s = new Socket("10.0.0.143", 3000); 
      } 

      output = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); 
      output.write("Android Connected"); 
      output.flush(); 

    //  new CommuTask().execute(); 

      //starting the thread 
      communication.start(); 
    //  s.close(); 
     } 

     catch(UnknownHostException e) 
     { 
      e.printStackTrace(); 
      try { 
       s.close(); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 
     } 

     catch(IOException e) 
     { 
      e.printStackTrace(); 
      try { 
       s.close(); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 
     } 

    } 
} 
+0

歡迎計算器,別忘了進入標記您的問題的習慣,當回答你已經找到了解決方案http://i.stack.imgur.com/uqJeW.png – Blundell

回答

0

取而代之的是:

mHandler.handleMessage(msg); 

使用此:

mHandler.sendMessage(msg); 
+0

謝謝!有效 :) – r0flonic

相關問題