简体   繁体   中英

Activity hangs on TextView.setText()

Code first:

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;
          }   
     }
}

The code above simply sends some message to a server and receives an answer. This answer should then be displayed in the TextView (see marked line). However, the app hangs at that line, ie, LogCat displays

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

Then no more lines are written to LogCat. Has anybody an explanation for that? If the marked line is commented out, LogCat looks like

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

如果您移动调用MyActivity.this.processAnswer()onPostExecute()代替,也许是可能的工作- IIRC,UI线程上的项目只能从UI线程更新。

First inialize your text view by following , then add onPostExecute method bellow the doInBackground method . And set your text there . Bellow is code which i change.

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);

    }   
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM