Handler的问题。线程退出未捕获的异常。

时间:2016-04-16 13:01:00

标签: java android multithreading

不要犹豫,这是我的第一个节目。我尝试将消息从一个线程发送到另一个线程,但是在接收时失败。这是崩溃的日志。

04-15 18:29:43.610 14765-15803/annikov.dd E/PositionConnection: Updating message: 50385
04-15 18:29:43.630 14765-15803/annikov.dd E/PositionConnection: mUpdateHandler { when=-9m0s379ms barrier=0 }
04-15 18:29:43.630 14765-15803/annikov.dd E/PositionConnection: mUpdateHandler.getdata Bundle[{pos=50385}]
04-15 18:29:43.640 14765-15803/annikov.dd W/dalvikvm: threadid=15: thread exiting with uncaught exception (group=0x4153abc0)
04-15 18:29:43.660 14765-15803/annikov.dd E/AndroidRuntime: FATAL EXCEPTION: Thread-194
                                                            Process: annikov.dd, PID: 14765
                                                            java.lang.NullPointerException
                                                                at annikov.dd.PositionConnection.updateMessages(PositionConnection.java:92)
                                                                at annikov.dd.PositionConnection$PositionClient$ReceivingThread.run(PositionConnection.java:242)
                                                                at java.lang.Thread.run(Thread.java:864)
04-15 18:29:43.970 14765-14808/annikov.dd I/NsdHelper: Discovery stopped: _http._tcp.
04-15 18:29:45.450 14765-14765/annikov.dd D/PositionClient: Socket getted
04-15 18:29:45.470 14765-14807/annikov.dd E/PositionConnection: Error creating ServerSocket: 
                                                                java.net.SocketException: Socket closed
                                                                    at libcore.io.Posix.accept(Native Method)
                                                                    at libcore.io.BlockGuardOs.accept(BlockGuardOs.java:55)
                                                                    at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:98)
                                                                    at java.net.ServerSocket.implAccept(ServerSocket.java:203)
                                                                    at java.net.ServerSocket.accept(ServerSocket.java:128)
                                                                    at annikov.dd.PositionConnection$PositionServer$ServerThread.run(PositionConnection.java:152)
                                                                    at java.lang.Thread.run(Thread.java:864)
04-15 18:29:45.470 14765-14807/annikov.dd W/System.err: java.net.SocketException: Socket closed
04-15 18:29:45.470 14765-14807/annikov.dd W/System.err:     at libcore.io.Posix.accept(Native Method)
04-15 18:29:45.470 14765-14807/annikov.dd W/System.err:     at libcore.io.BlockGuardOs.accept(BlockGuardOs.java:55)
04-15 18:29:45.480 14765-14807/annikov.dd W/System.err:     at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:98)
04-15 18:29:45.480 14765-14807/annikov.dd W/System.err:     at java.net.ServerSocket.implAccept(ServerSocket.java:203)
04-15 18:29:45.480 14765-14807/annikov.dd W/System.err:     at java.net.ServerSocket.accept(ServerSocket.java:128)
04-15 18:29:45.490 14765-14807/annikov.dd W/System.err:     at annikov.dd.PositionConnection$PositionServer$ServerThread.run(PositionConnection.java:152)
04-15 18:29:45.490 14765-14807/annikov.dd W/System.err:     at java.lang.Thread.run(Thread.java:864)
04-15 18:34:44.160 14765-15803/annikov.dd I/Process: Sending signal. PID: 14765 SIG: 9

它是发件人的代码部分:

public class PositionConnection {

    private Handler mUpdateHandler;

...

public synchronized void updateMessages(int position, boolean local) {
    Log.e(TAG, "Updating message: " + position);


    Bundle messageBundle = new Bundle();
    messageBundle.putInt("pos", position);

    Message message = new Message();
    message.setData(messageBundle);
    Log.e(TAG, "mUpdateHandler " + message);
    Log.e(TAG, "mUpdateHandler " + message.getData());
    mUpdateHandler.sendMessage(message);              <---here it is crashed
    //mUpdateHandler.sendEmptyMessage(30450);


}

这是来自接收者:

public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener, SensorEventListener, NsdHelper.DiscoverListener {
    private static final String TAG = "MainActivity";
    private SeekBar channel4PositionBar, channel5PositionBar, channel6PositionBar;
    private MaestroSSC maestroSSC;

    NsdHelper mNsdHelper;
    PositionConnection mConnection;
    private Handler mUpdateHandler;

...

mUpdateHandler = new Handler()

{
    @Override
    public void handleMessage(Message msg) {
        int position = msg.getData().getInt("pos");

        if (position >= 50000) {
            maestroSSC.setTarget(5, position - 50000);
            servo2value.setText(String.valueOf(position - 50000));
            channel6PositionBar.setProgress(position - 50000);
        } else if (position >= 40000) {
            maestroSSC.setTarget(4, position - 40000);
            servo1value.setText(String.valueOf(position - 40000));
            channel5PositionBar.setProgress(position - 40000);
        } else if (position >= 30000) {
            pwmvalue.setText(String.valueOf(position - 30000));
            maestroSSC.setTarget(3, position - 30000 + 1200);
            channel4PositionBar.setProgress(position - 30000);
        }
        //setSeekBarProgressIgnoreListener(position);
    }
};

有谁可以弄清楚这里有什么问题?我真的不知道如何继续进行*(

1 个答案:

答案 0 :(得分:0)

NullPointerException处有一个annikov.dd.PositionConnection.updateMessages(PositionConnection.java:92)。对于mUpdateHandler

updateMessages代码,PositionConnection为空是唯一的可能性