活动在TextView.setText()上挂起

时间:2014-10-11 12:52:17

标签: android

代码优先:

public class MyActivity extends Activity {

    Button   send;
    TextView textv;
    String   answer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

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

        send  = (Button)findViewById(R.id.sendButton);
        textv = (TextView)findViewById(R.id.textViewv);

        send.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {       
                MyClientTask myClientTask = new MyClientTask("localhost", 1234, "QUESTION");
                myClientTask.execute();
            }
        });     
    }


    void processAnswer() {
        Log.i("DEBUG", "in processAnswer - before setting text");
        Log.i("DEBUG", "ANSWER");

        textv.setText("ANSWER\n");  // <-------- H E R E -----------

        Log.i("DEBUG", "in processAnswer - after setting text");
    }



    public class MyClientTask extends AsyncTask<Void, Void, Void> {

          String dstAddress;
          int dstPort;

          String message;
          String response;

          MyClientTask(String addr, int port, String msg){
           dstAddress = addr;
           dstPort    = port;
           message    = msg;
           response   = "";
          }

          @Override
          protected Void doInBackground(Void... arg0) {

           Socket socket = null;   

            try {
                InetAddress serverAddr = InetAddress.getByName(dstAddress);

                socket = new Socket(serverAddr, dstPort);
                OutputStream out = socket.getOutputStream();     
                out.write(message.getBytes());
                out.flush();

                String msgrc = "";
                int charsRead = 0;
                char[] inputBuf = new char[4096];

                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader in = new BufferedReader(isr);
                while ((charsRead = in.read(inputBuf)) != -1) {
                        msgrc += new String(inputBuf).substring(0, charsRead);
                }

                // outer class variable
                MyActivity.this.answer = msgrc;

                out.close();
                is.close();
                socket.close();


                Log.i("DEBUG", "before processing answer");
                MyActivity.this.processAnswer();
                Log.i("DEBUG", "after processing answer");

            } catch (Exception e) {

            }

            return null;
          }   
     }
}

上面的代码只是向服务器发送一些消息并收到答案。然后,此答案应显示在TextView中(请参阅标记的行)。但是,应用程序挂起该行,即LogCat显示

[...]
before processing answer
in processAnswer - before setting text
ANSWER

然后不再向LogCat写入任何行。对此有任何解释吗?如果标记的行被注释掉,LogCat看起来像

[...]
before processing answer
in processAnswer - before setting text
ANSWER
in processAnswer - after setting text
after processing answer

2 个答案:

答案 0 :(得分:0)

如果您将来电转移到MyActivity.this.processAnswer()而不是onPostExecute(),或许可能有效 - IIRC UI 主题上的项目应仅从UI线程更新。

答案 1 :(得分:0)

首先通过以下方式对文本视图进行初始化,然后在doInBackground下添加onPostExecute方法 方法 。并在那里设置你的文字。贝娄是我改变的代码。

public class MyActivity extends Activity {

Button   send;
TextView textv;
String   answer;

@Override
protected void onCreate(Bundle savedInstanceState) {

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

    send = (Button)findViewById(R.id.sendButton);
    textv = (TextView)findViewById(R.id.textview);

    send.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {       
            MyClientTask myClientTask = new MyClientTask("localhost", 1234, "QUESTION");
            myClientTask.execute();
        }
    });     
}



public class MyClientTask extends AsyncTask<Void, Void, Void> {

      String dstAddress;
      int dstPort;

      String message;
      String response;

      MyClientTask(String addr, int port, String msg){
       dstAddress = addr;
       dstPort    = port;
       message    = msg;
       response   = "";
      }

      @Override
      protected Void doInBackground(Void... arg0) {

       Socket socket = null;   

        try {
            InetAddress serverAddr = InetAddress.getByName(dstAddress);

            socket = new Socket(serverAddr, dstPort);
            OutputStream out = socket.getOutputStream();     
            out.write(message.getBytes());
            out.flush();

            String msgrc = "";
            int charsRead = 0;
            char[] inputBuf = new char[4096];

            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader in = new BufferedReader(isr);
            while ((charsRead = in.read(inputBuf)) != -1) {
                    msgrc += new String(inputBuf).substring(0, charsRead);
            }

            // outer class variable
            MyActivity.this.answer = msgrc;

            out.close();
            is.close();
            socket.close();


            Log.i("DEBUG", "before processing answer");
            MyActivity.this.processAnswer();
            Log.i("DEBUG", "after processing answer");

        } catch (Exception e) {

        }

        return null;
      }   


      @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        textv.setText(msgrc);

    }   
 }
}