-1

我正在使用本指南:

将套接字编程结合到您的应用程序中

在我的这部分代码中,我试图让服务器发送消息以响应 UI 按钮按下:

public class MusicServerThread implements Runnable {
        Socket client;

        public void run() {
            try {
                if (SERVERIP != null) { //'Success' case when there is an Internet connection.
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Log.d(WiFiDirectActivity.TAG, "MusicServerThread_run: ServerIP = " + SERVERIP);
                            //TODO serverStatus can't be null. Fix this.
                            //serverStatus.setText("Listening on IP: " + SERVERIP);
                        }
                    });
                    serverSocket = new ServerSocket(SERVERPORT);
                    Log.d(WiFiDirectActivity.TAG, "MusicServerThread_run: serverSocket created on port " + SERVERPORT);
                    while (true) {
                        // listen for incoming clients
                        client = serverSocket.accept();
                        Log.d(WiFiDirectActivity.TAG, "MusicServerThread_run: Client connection recieved.");
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                //serverStatus.setText("Connected.");
                            }
                        });

                        try {
                            //This line takes the client commands.
                            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                            String line = null;
                            while ((line = in.readLine()) != null) {
                                //TODO Stop endless loop from being a radge. Play music command
                                Log.d(WiFiDirectActivity.TAG, "Command issued " + line);
                                if (line.toLowerCase(Locale.getDefault()).trim().equals("play")){ //if the command is play
                                    Log.d(WiFiDirectActivity.TAG, "Server says: client command accepted " + line);

                                    //MediaPlayer mediaPlayer = MediaPlayer.create(getActivity(), R.raw.mse5251);
                                    //mediaPlayer.start(); // no need to call prepare(); create() does that for you

                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            // do whatever you want to the front end
                                            // this is where you can be creative
                                            MediaPlayer mediaPlayer = MediaPlayer.create(getActivity(), R.raw.metallica_guitar);
                                            mediaPlayer.start(); // no need to call prepare(); create() does that for you


                                        }
                                    });
                                }

                                if (line.toLowerCase(Locale.getDefault()).trim().equals("join")){ //if the command is join
                                    Log.d(WiFiDirectActivity.TAG, "Server says: client command accepted " + line);

                                    //Just for fun server will play as well.
                                    //Todo: add a timer to delay start
                                    MediaPlayer mediaPlayer = MediaPlayer.create(getActivity(), R.raw.metallica_guitar);
                                    mediaPlayer.start(); // no need to call prepare(); create() does that for you

                                    try {
                                        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client
                                                .getOutputStream())), true);
                                        // where you issue the commands
                                        out.println("play");
                                    } catch (Exception e) {
                                        Log.e(WiFiDirectActivity.TAG, "S: Error", e);
                                    }

                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            // do whatever you want to the front end
                                            // this is where you can be creative



                                        }
                                    });
                                }

                                break;// Play command received, break.
                            }
                            break;
                        } catch (Exception e) {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    //serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
                                }
                            });
                            e.printStackTrace();
                        }
                    }
                } else {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            //serverStatus.setText("Couldn't detect internet connection.");
                        }
                    });
                }
            } catch (Exception e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        //serverStatus.setText("Error");
                    }
                });
                e.printStackTrace();
            }
        }

        /*Method used by the server to send command strings.*/
        public void sendCommand(String command){
            Log.d(WiFiDirectActivity.TAG, "MusicServerThread_sendCommand: Method reached.");

            try {
                PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client
                        .getOutputStream())), true);
                // where you issue the commands
                out.println("play");
            } catch (Exception e) {
                Log.e(WiFiDirectActivity.TAG, "S: Error", e);
            }
        }

但是我在我的 SendCommands 方法中收到了我认为是客户端的空指针异常。

甚至可以让服务器线程运行,等待命令,而我尝试使用它的 SendCommands 方法向客户端发送命令?

更新:

这是错误的堆栈跟踪:

03-04 16:35:44.055: E/wifidirectdemo(4202): S: Error
03-04 16:35:44.055: E/wifidirectdemo(4202): java.lang.NullPointerException
03-04 16:35:44.055: E/wifidirectdemo(4202):     at com.example.android.wifidirect.DeviceDetailFragment$MusicServerThread.sendCommand(DeviceDetailFragment.java:465)
03-04 16:35:44.055: E/wifidirectdemo(4202):     at com.example.android.wifidirect.DeviceDetailFragment$4.onClick(DeviceDetailFragment.java:201)
03-04 16:35:44.055: E/wifidirectdemo(4202):     at android.view.View.performClick(View.java:3538)
03-04 16:35:44.055: E/wifidirectdemo(4202):     at android.view.View$PerformClick.run(View.java:14330)
03-04 16:35:44.055: E/wifidirectdemo(4202):     at android.os.Handler.handleCallback(Handler.java:608)
03-04 16:35:44.055: E/wifidirectdemo(4202):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-04 16:35:44.055: E/wifidirectdemo(4202):     at android.os.Looper.loop(Looper.java:156)
03-04 16:35:44.055: E/wifidirectdemo(4202):     at android.app.ActivityThread.main(ActivityThread.java:4977)
03-04 16:35:44.055: E/wifidirectdemo(4202):     at java.lang.reflect.Method.invokeNative(Native Method)
03-04 16:35:44.055: E/wifidirectdemo(4202):     at java.lang.reflect.Method.invoke(Method.java:511)
03-04 16:35:44.055: E/wifidirectdemo(4202):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-04 16:35:44.055: E/wifidirectdemo(4202):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-04 16:35:44.055: E/wifidirectdemo(4202):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

3

我的猜测是你sendCommand之前的调用client已经初始化,导致NullPointerException上线:

PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
                    client.getOutputStream())), true);

因为client是空的。

根据堆栈跟踪,从类中的方法sendCommand调用,第 201 行。onClickDeviceDetailFragment

您的主要选择是:

  • 输入时sendCommand,请确保它client不为空,如果是,请让用户知道您尚未连接。
  • 禁用该onClick方法(例如通过禁用相应的按钮),直到建立连接并且client不为空
于 2013-03-05T11:45:11.203 回答