Android Socket with esp8266 communication error
# when click the button to send data appeared the following error:
Small white for help!
Press the button error:
E/AndroidRuntime: FATAL EXCEPTION: the main
Process: com. Mywifi. Tyf. Cn. Wifi2, PID: 8143
Java. Lang. NullPointerException: Attempt to invoke virtual method 'void Java IO. BufferedWriter. Write (Java. Lang. String)' on a null object reference
At com. Mywifi. Tyf. Cn. Wifi2. MainActivity2. The onClick (MainActivity. Java: 63) atandroid. View. The view. The performClick (view. Java: 5637) atandroid. The View2. The onClick (MainActivity. Java: 63) at android. View. The view. The performClick (view. Java: 5637) at android. The View2. The onClick (MainActivity. Java: 63) atandroid. View. The view. The performClick (view. Java: 5637) atandroid. The ViewPerformClick. Run (22429) view. Java:
An android. OS. Handler. HandleCallback (Handler. Java: 751)
An android. OS. Handler. DispatchMessage (Handler. Java: 95)
. An android OS. Stars. Loop (154). Which Java:
. An android app. ActivityThread. Main (6119) ActivityThread. Java:
The at Java. Lang. Reflect. Method. Invoke (Native Method)
At com. Android. Internal. OS. $MethodAndArgsCaller ZygoteInit. Run (ZygoteInit. Java: 886)
At com. Android. Internal. OS. ZygoteInit. Main (ZygoteInit. Java: 776)
The source code
Public class MainActivity extends the Activity {
Private EditText IP and Port;
Private Button Link, Butt1 Butt2;
Private Socket Socket=null;
Private BufferedWriter writer=null;
@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IP=(EditText) the findViewById (R.i d.i p);
Port=(EditText) the findViewById (R.i d.p ort);
The Link=(Button) the findViewById (R.i d.l ink);
Butt1=(Button) the findViewById (R.i db utt1);
Butt2=(Button) the findViewById (R.i db utt2);
The Link. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View v) {
If (socket==null) {
String IP=IP. GetText (). The toString ();
Int port=Integer. The valueOf (port. The getText (), toString ());
MyThread MyThread=new MyThread (IP, port);
MyThread. Start ();
} the if (socket!=null) {
Try {
socket.close();
The socket=null;//to empty mSocket
The Link. The setText (" connection ");
Toast. MakeText (MainActivity. This connection has been closed, Toast. LENGTH_LONG), show ();
} the catch (IOException e) {
e.printStackTrace();
}
}
}
});
In/on/
Butt1. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View v) {
//ThreadSendData (" 0 ");
Try {
Writer. Write (0 + "\ n");
writer.flush();
} the catch (IOException e) {
e.printStackTrace();
}
Toast. MakeText (MainActivity. This, "sent successfully," Toast. LENGTH_SHORT), show ();
}
});
//off
Butt2. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View v) {
ThreadSendData (" 1 ");
Toast. MakeText (MainActivity. This, "sent successfully," Toast. LENGTH_SHORT), show ();
}
});
}
/* connection thread/
Private class MyThread extends Thread {
private String ip;
private int port;
IP public MyThread (String, int port) {
This. IP=IP;
This. The port=port;
}
Public void the run () {
Try {
The socket=new socket (IP, port);
Writer=new BufferedWriter (new OutputStreamWriter (socket. GetOutputStream ()));
Toast. MakeText (MainActivity. This, "sent successfully," Toast. LENGTH_SHORT), show ();
RunOnUiThread (new Runnable () {
@ Override
Public void the run () {
The Link. The setText (" connected ");
Toast. MakeText (MainActivity. This, "connected", Toast. LENGTH_SHORT), show ();
}
});
} the catch (IOException e) {
e.printStackTrace();
RunOnUiThread (new Runnable () {
@ Override
Public void the run () {
Toast. MakeText (MainActivity. This "connection", Toast. LENGTH_SHORT), show ();
}
});
}
}
}
/* used to send data/
Public void ThreadSendData (String num) {
Try {
Writer. Write (num + "\ n");
writer.flush();
} the catch (IOException e) {
e.printStackTrace();
}
}
}
Ask god to help give directions, searched lots of information also didn't find a clue,
Finally released in: 2019-11-23