-1

テキストビューでタッチ位置を取得できるAndroidプログラムがありますが、ソケットで座標調整を送信すると、プログラムはそれを1回送信して直接クラッシュします。

何か考えはありますか?

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 コネクタ車の受信デュ メッセージ: 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:

私が持っているジョップの編集で:

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");
        }
    };
};*/

}

4

1 に答える 1

0

You cannot use networking primitives on the main event dispatch thread, as this might block the UI. See the documentation and this other question.

In short, you should use a background thread to read from the socket and execute a callback when the request is finished. Assuming that you change ClientSocket as sketeched below:

public class ClientSocket extends Thread {

    public static interface ReplyHandler {
        public void handleReply(String line);
         // more methods, if you need them... (e.g., handleException)
    };

    private ReplyHandler cb;

    // ...

    public static String requete(String msg, ReplyHandler cb) throws IOException {
        this.cb = cb;
        out.println(msg);
    }

    public void run() {
        while(...) {
            String line = in.readLine();
            cb.handleReply(line);
        }
    }

You can use it like this:

    ClientSocket.requete("SC 05 "+String.valueOf( x ), new ReplyHandler() {
        public void handleReply(String line) {
            Log.d("Message", "Message caméra envoyé");
        }
    );

Note that, to make it work, handle exceptions, start the thread, and so on. In particular, you might want to add a second method to the ReplyHandler interface to handle exceptions.

于 2013-06-12T08:44:26.447 に答える