无法在textView中写入值

时间:2016-06-12 18:56:18

标签: java android sockets android-fragments

在这个片段(Android)中,当我发送消息GR时,我必须收到像GR20这样的答案,我想在textView中可视化。代码不起作用。 我看不出错误,我尝试了一些方法,但我无法解决。 有人可以帮我吗?

    package com.example.nicolarinaldi.myapplication;


    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.SeekBar;
    import android.widget.TextView;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;


    public class TabFragment2 extends Fragment {


        private Socket socket;
        // private static final int SERVERPORT = 60000;
        //private static final String SERVER_IP = "172.17.8.104";
        private static final int SERVERPORT = 60000;
        private static final String SERVER_IP = "192.168.1.3";
        int temperatura_termostato = 0;




        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.tab_fragment_2, container, false);
            SeekBar seekBar = (SeekBar)rootView.findViewById(R.id.seekBar);
            final TextView textView5 = (TextView)rootView.findViewById(R.id.textView5);
            final TextView textView6 = (TextView)rootView.findViewById(R.id.textView6);

            try {
                new Thread(new ClientThread()).start();
                PrintWriter out = new PrintWriter(new BufferedWriter(
                        new OutputStreamWriter(socket.getOutputStream())),
                        true);

                out.write("GR");
                out.flush();
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String gi = in.readLine();
                byte[] temperatura = gi.getBytes();
                textView6.setText(String.valueOf(temperatura));

            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

            //textView6.setText(String.valueOf(temperatura));
            seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                                              boolean fromUser) {
                    // TODO Auto-generated method stub
                    textView5.setText(String.valueOf(progress));

                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    new Thread(new ClientThread()).start();
                    try {
                        PrintWriter out = new PrintWriter(new BufferedWriter(
                                new OutputStreamWriter(socket.getOutputStream())),
                                true);

                        out.write("SR" + temperatura_termostato);
                        out.flush();

                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            });

            return rootView;
        }
        class ClientThread implements Runnable {
            @Override
            public void run() {
                try {
                    InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
                    socket = new Socket(serverAddr, SERVERPORT);
                } catch (UnknownHostException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }


    }

1 个答案:

答案 0 :(得分:0)

这是因为您在单独的线程中创建套接字连接。在读取/写入套接字之前,甚至没有创建套接字的可能性很高。您必须在此处创建一个读写器线程,一旦套接字连接完成,它将得到通知。

此后,您可以在套接字上读/写。