Android套接字-从Stream读取对象后,应用程序崩溃

时间:2018-09-02 13:00:08

标签: java android multithreading sockets stream

我正在尝试通过Android(Java)中的套接字将对象从客户端发送到服务器。 但是,一旦从客户端收到对象,应用程序(SERVER)就会崩溃。 我的客户端代码侦听触摸事件,并根据这些触摸事件在Action上流传输特定对象(ObjectOutputStream),但是一旦我将手指放在客户端的屏幕上,触摸事件被触发,Action对象在流上发送,并且服务器在接收到该对象时崩溃。

服务器端代码

 @Override
        protected Void doInBackground(Void... voids) {
            ObjectInputStream stream;

            try{
                server = new ServerSocket(3107);
                socket = server.accept();
                stream = new ObjectInputStream(socket.getInputStream());
                publishProgress(new Action(0,0,CONNECTED)); /*TO KNOW IF CLIENT HAS CONNECTED*/

                while(true)
                {
                    i++;
                    if(isCancelled()){
                        break;
                    }
                    action = (Action)stream.readObject();
                    publishProgress(action);
                }
            }catch (Exception exception)
            {
                exception.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onProgressUpdate(Action... actions) {
            super.onProgressUpdate(actions);
            if(actions[0].type==CONNECTED)
            {
                Toast.makeText(MainActivity.getContext(),"CONNECTED",Toast.LENGTH_SHORT).show();
            }
            MainActivity.context.MyCustomDrawingView.draw(actions[0]);
        }

客户端代码

@Override
        public boolean onTouchEvent(MotionEvent event) {
            final float x = event.getX();
            final float y = event.getY();
            final int ACTION;

                switch (ACTION = event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        touch_start(x, y);
                        invalidate();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        touch_move(x, y);
                        invalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        touch_up();
                        invalidate();
                        break;
                }

                    Thread thread = new Thread(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                stream.writeObject(new Action(x, y, ACTION));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    thread.start();
            return true;
        }
    }

LOGCAT(错误)

09-02 18:59:52.426 29234-29234/? E/Zygote: isWhitelistProcess - Process is Whitelisted
09-02 19:00:08.958 29234-29234/com.nitishprajapati.server_b E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.nitishprajapati.server_b, PID: 29234
    java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.ObjectInputStream.close()' on a null object reference
        at com.nitishprajapati.server_b.MainActivity$ServerTask.onPostExecute(MainActivity.java:252)
        at com.nitishprajapati.server_b.MainActivity$ServerTask.onPostExecute(MainActivity.java:185)
        at android.os.AsyncTask.finish(AsyncTask.java:695)
        at android.os.AsyncTask.-wrap1(Unknown Source:0)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

编辑: 好的,现在该应用程序没有崩溃。问题是我有两个名为stream的变量。 doInBackground()中的一个和AsyncTask正文中的一个。变量混乱导致NullPointerException。但是现在还有另一个问题,当我在客户端上执行触摸事件时,服务器直接执行onPostExecute()。这意味着它正在break从doInBackground()

0 个答案:

没有答案