null套接字上的getOutputStream?

时间:2017-06-12 12:44:57

标签: java android multithreading sockets

我是网站上的新人,我在这里问一个关于套接字和线程的问题。我有这个代码,它给了我以下错误:

    public class Conferma extends AppCompatActivity {

    private final String TAG = "Conferma";
    public Socket socket;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.conferma);

        final EditText etTavolo = (EditText) findViewById(R.id.etTavolo);
        final RadioButton rbSi = (RadioButton) findViewById(R.id.rbSi);
        RadioButton rbNo = (RadioButton) findViewById(R.id.rbNo);
        Button bInvia = (Button) findViewById(R.id.bInvia);

        Thread t = new Thread(new ClientThread());
        t.start();

        bInvia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String ordine = getIntent().getExtras().getString("ordine");
                if(etTavolo.getText().toString().equals("")){
                    AlertDialog.Builder builder = new AlertDialog.Builder(Conferma.this);
                    builder.setMessage("Inserire il numero del tavolo!").setNegativeButton("Riprova!", null).create().show();
                }
                ordine = ordine + "Tavolo: " + etTavolo.getText().toString() + "/";
                if(rbSi.isChecked()) {
                    ordine = ordine + "Coperto: Sì/";
                }
                else{
                    ordine = ordine + "Coperto: No/";
                }
                Log.d(TAG, ordine);

                //invio informazione
                inviaStringa(view, ordine);
            }
        });
    }

    private void inviaStringa(View view, String ordine) {

        try{
            OutputStream s = socket.getOutputStream();
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(s);
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            PrintWriter out = new PrintWriter(bufferedWriter, true);
            out.println(ordine);
        } catch (Exception e) {
            Log.e(TAG, "Errore: " + e);
        }
    }

    private class ClientThread implements Runnable {

        private static final int SERVER_PORT = 6000;
        private static final String SERVER_IP = "109.115.84.90";
        private static final String TAG = "ClientThread";

        @Override
        public void run() {
            try {
                InetAddress serverAddress = InetAddress.getByName(SERVER_IP);
                Log.d(TAG, "creato");
                socket = new Socket(serverAddress, SERVER_PORT);
            } catch (UnknownHostException e) {
                Log.e(TAG, "Errore: " + e);
            } catch (IOException e) {
                Log.e(TAG, "Errore: " + e);
            }
        }
    }
}

错误:

  

java.lang.NullPointerException:尝试调用虚方法' java.io.OutputStream java.net.Socket.getOutputStream()'在空对象引用上

现在,从错误我认为问题出现在Socket中,这意味着没有初始化。但是如果我在创建简单线程的过程中调用ClientThread方法,为什么不进行初始化?感谢您的帮助,对不起,如果我不能比这更清楚! :((

1 个答案:

答案 0 :(得分:0)

实现这一目标的最佳(也是最正确的)方法是使用AsyncTask(正如我在评论中所写)。

如果你想使用那个类,那么有很多解决方案,在这里我只是建议我认为更清洁,在另一个文件中创建ClientThread类并添加方法inviaStringa:

<强> ClientThread:

private class ClientThread implements Runnable {    
   private static final int SERVER_PORT = 6000;
         private static final String SERVER_IP = "109.115.84.90";
            private static final String TAG = "ClientThread";
        private Socket socket;

    public ClientThread(){
                try {
                    InetAddress serverAddress = InetAddress.getByName(SERVER_IP);
                    Log.d(TAG, "creato");
                    socket = new Socket(serverAddress, SERVER_PORT);
                } catch (UnknownHostException e) {
                    Log.e(TAG, "Errore: " + e);
                } catch (IOException e) {
                    Log.e(TAG, "Errore: " + e);
                }
            }

    }

            @Override
            public void run() {
    }
            private void inviaStringa(View view, String ordine) {

            try{
                OutputStream s = socket.getOutputStream();
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(s);
                BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
                PrintWriter out = new PrintWriter(bufferedWriter, true);
                out.println(ordine);
            } catch (Exception e) {
                Log.e(TAG, "Errore: " + e);
            }
        }

        }

然后在 Conferma 内你不需要更多来声明套接字,但只是这样做

Thread t = new Thread(new ClientThread());
        t.start();

...//when you've to send the String

t.inviaStringa(view, ordine);