私を躊躇せず、私の最初のプログラムです。あるスレッドから別のスレッドにメッセージを送信しようとしますが、受信に失敗します。クラッシュのログがあります。Handlerに関する問題。 catchされない例外で終了するスレッド。
...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;
:
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);
}
そして、これは受信機からです
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);
}
};
ここで何が間違っているのか分かりますか?私は本当に、さらには、*(
のためのあなたの
updateMessages
コードを持つ唯一の可能性であります? –