Android的NetowrkOnMainThreadException [英] Android NetowrkOnMainThreadException
问题描述
的你好!的
我尝试建立一个Android应用程序来控制无人机的Ar。
但我有一个问题,当我尝试我的设备与无人机连接。
我创建了一个线程做一个插座。但是,当我在主要活动运行它,我有一个例外: android.os.NetworkOnMainThreadException
这是我的code。与我的ThreadCmd类主题:
字符串COMMANDE =;
字节[] cmdToByte;
串iPinString;
InetAddress类IpDrone;
DatagramSocket的ClientSocket的;
螺纹threadDecollage;
TextView的电视;
布尔政变;
DatagramPacket类sendPacketWithCmd;
私人字节[] IP = {(字节)192,(字节)168(字节)1,(字节)1};
布尔isRunning = TRUE;
私人最终静态长TIME_SLEEP = 20;
公共无效的run(){
尝试{
IpDrone = InetAddress.getByAddress(IP);
}赶上(E1的UnknownHostException){
e1.printStackTrace();
政变= FALSE;
}
尝试{
ClientSocket的=新的DatagramSocket();
}赶上(SocketException E){
e.printStackTrace();
政变= FALSE;
} 而(isRunning){
运行控制();
尝试{
视频下载(TIME_SLEEP);
}赶上(InterruptedException的E){
e.printStackTrace();
}
} }
公共无效的运行控制(){ COMMANDE =AT * REF = 1,290717696< LF>对于* REF = 2,290717952< LF>对于* REF = 3,290717696< LF>中;
cmdToByte = commande.getBytes();
尝试{ sendPacketWithCmd =新的DatagramPacket(cmdToByte,cmdToByte.length,IpDrone,5556);
clientSocket.send(sendPacketWithCmd);
}赶上(IOException异常五){
e.printStackTrace();
政变= FALSE;
}
政变= TRUE;
isRunning = FALSE;
}
公共布尔retourneetat(){
返回政变;
}
在后的主要活动:
Threadcmd TC =新Threadcmd();
tc.run();
我没有看到我的错误,所以我问你,如果你能帮助我! (我不会忘记的权限)
PS:请原谅我的英语:)
tc.run();
这是你的问题。在这里,您调用相同的线程上的run()方法。
为了真正开始一个新的线程,通话 tc.start();
Hi !!
I try to build an Android Application to control the Ar drone. But i have a problem when i try to connect my device with the drone. I created a Thread to do a socket. But when i run it in the main activity i have a exception : android.os.NetworkOnMainThreadException .
This is my code with my Thread in ThreadCmd class:
String commande="";
byte[] cmdToByte;
String iPinString;
InetAddress IpDrone;
DatagramSocket clientSocket;
Thread threadDecollage;
TextView tv ;
boolean etat;
DatagramPacket sendPacketWithCmd;
private byte[] ip = {(byte)192, (byte)168, (byte)1, (byte)1 };
boolean isRunning = true;
private final static long TIME_SLEEP= 20;
public void run() {
try {
IpDrone = InetAddress.getByAddress(ip);
} catch (UnknownHostException e1) {
e1.printStackTrace();
etat = false;
}
try {
clientSocket = new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
etat = false;
}
while(isRunning){
runControl();
try {
Thread.sleep(TIME_SLEEP);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void runControl(){
commande ="AT*REF=1,290717696<LF>AT*REF=2,290717952<LF>AT*REF=3,290717696<LF>";
cmdToByte = commande.getBytes();
try {
sendPacketWithCmd = new DatagramPacket(cmdToByte,cmdToByte.length,IpDrone,5556);
clientSocket.send(sendPacketWithCmd);
} catch (IOException e) {
e.printStackTrace();
etat = false;
}
etat = true;
isRunning=false;
}
public boolean retourneetat(){
return etat;
}
After in the main activity :
Threadcmd tc = new Threadcmd();
tc.run();
I dont see my error so i ask to you if you can help me !! ( i don't forget the permission)
ps: please excuse my english :)
tc.run();
This is your problem. Here you call the run() method on the same thread.
In order to actually start a new thread, call
tc.start();
这篇关于Android的NetowrkOnMainThreadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!