我有一个 android 程序,它可以在 textview 上获取触摸位置,但是当我通过套接字发送坐标协调时,程序发送一次并直接崩溃。
你有什么主意吗 ?
java文件中的代码。
final TextView tvCamera = (TextView)findViewById(R.id.textViewCamera);
// this is the view on which you will listen for touch events
final View touchView = findViewById(R.id.touchViewCamera);
touchView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) (event.getX()-150);
int y = (int) (event.getY()-150)*(-1);
if (x<-150)
x=-150;
else if (x>150)
x=150;
if (y<-150)
y=-150;
else if (y>150)
y=150;
tvCamera.setText("Coordonnées Camera : (X:Y) "+String.valueOf(x) + " : " + String.valueOf(y));
try {
ClientSocket.requete("SC 05 "+String.valueOf( x ));
Log.d("Message", "Message caméra envoyé");
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d("Message", "Message caméra echec");
e.printStackTrace();
}
return true;
}
});
请求文件 .java 中的代码
public class ClientSocket {
private static Socket socket;
private static BufferedReader in;
private static PrintWriter out;
public ClientSocket(String nomSrv, int port) throws Exception {
socket = new Socket(nomSrv, port);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public static String requete(String msg) throws IOException {
out.println(msg);
String line = in.readLine();
return line;
}
public static void fermeture() throws IOException{
in.close();
out.close();
socket.close();
}
}
当我触摸屏幕并发送“ClientSocket.requete("SC 05"+String.valueOf(x));" 我有问题: String line = in.readLine();
报告标签:
eglSurfaceAttrib 未实现 06-12 08:25:14.255:D/OpenGLRenderer(785):启用调试模式 0 06-12 08:25:16.134:I/System.out(785):Vous ete 连接器汽车接收 du 消息:S0 06-12 08:25:16.134: I/System.out(785): Connexion établie avec le serverur !06-12 08:25:16.134: I/System.out(785): S0 06-12 08:25:17.143: E/InputEventReceiver(785): 异常调度输入事件。06-12 08:25:17.143:E/MessageQueue-JNI(785):MessageQueue 回调中的异常:handleReceiveCallback 06-12 08:25:17.244:E/MessageQueue-JNI(785):android.os.NetworkOnMainThreadException 06-12 08:25:17.244: E/MessageQueue-JNI(785): 在 android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 06-12 08:25:17.244: E/MessageQueue-JNI(785):在 libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163) 06-12 08:25:17.244:
Jop的编辑我有:
public class ClientSocket extends Thread {
private static Socket socket;
private static BufferedReader in;
private static PrintWriter out;
private ReplyHandler cb;
public ClientSocket(String nomSrv, int port) throws Exception {
socket = new Socket(nomSrv, port);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public static String requete(String msg) throws IOException {
out.println(msg);
String line = in.readLine();
return line;
}
public static void fermeture() throws IOException{
in.close();
out.close();
socket.close();
}
public static interface ReplyHandler {
public void handleReply(String line);
// more methods, if you need them... (e.g., handleException)
};
public String requete(String msg, ReplyHandler cb) throws IOException {
this.cb = cb;
out.println(msg);
return msg;
}
public void run() {
while(true) {
String line = null;
try {
line = in.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cb.handleReply(line);
}
}
/*private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if(msg.what == 0) {
monBouton.setText("C'est bon");
}
};
};*/
}