UDP下Socket通信 编译生成的APK程序无法运行

开源bao 发布于 2013/05/10 16:51
阅读 261
收藏 0

ARM开发板(linux系统)通过Wi-Fi采用UDP协议向Android手机发送数据(ARM开发板已经和Android手机连成局域网,相互可以Ping通) ,编译完成的APK文件,安装到手机中,点击报错(程序停止运行)刚开始学习Android 请各位大神指教   万分感谢!!!!!!代码如下

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
 private TextView myTextView = null;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  myTextView=(TextView)findViewById(R.id.mytextview);
  Integer port=6666;
  InetAddress ip = null;
  try {
    ip = InetAddress.getLocalHost();
  } catch (UnknownHostException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
  byte[] message = new byte[1024];
   try{
   DatagramSocket datagramSocket = new DatagramSocket(port,ip);
   DatagramPacket datagramPacket = new DatagramPacket(message,message.length);
  
   try{
    while(true){
     datagramSocket.receive(datagramPacket);
     String getMs=new String(message,0,datagramPacket.getLength());
     myTextView.setText(getMs);
         }
   } catch(IOException e){
    e.printStackTrace();
   }
  }catch (SocketException e){
   e.printStackTrace();
  }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

 

 

 

加载中
返回顶部
顶部