Home > Mobile >  Android Socket to solve
Android Socket to solve

Time:10-05

Package com. Example. Administrator. Myapplication;

The import android. Support. V7. App. AppCompatActivity;
The import android. OS. Bundle;
The import android. View. The view;
The import android. Widget. The Button;
The import android. Widget. The EditText;

Import the Java. IO. DataOutputStream;
Import the Java. IO. IOException;
Import the Java. IO. OutputStream;
The import java.net.Socket;

Public class MainActivity extends AppCompatActivity {

Private EditText e1;
Private Button b1.
Private Socket s=null;
Private OutputStream out;
Private String values.

@ Override
Protected void onCreate (Bundle savedInstanceState) {
Super. OnCreate (savedInstanceState);
The setContentView (R.l ayout. Activity_main);
InitView ();
Thread. The start ();
}
Public void initView () {
E1=the findViewById (R.i d.e dit);
B1=the findViewById (R.i db tn);
B1. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View v) {
Values=e1. GetText (). The toString ();
Try {
Out. Write (values. GetBytes ());
} the catch (IOException e) {
e.printStackTrace();
} the finally {
E1. SetText (" ");
}
}
});
}
Thread the Thread=new Thread (new Runnable () {
@ Override
Public void the run () {
If (s==null) {
Try {
S=new Socket (" 172.18.11.103 ", 8000);
Out=s.g etOutputStream ();
} the catch (Exception e) {
e.printStackTrace();
}
}
}
});
}

Requirements: from the EditText characters, sent via sockets,
Network access also added, click will collapse, where is there a problem?

CodePudding user response:

See the error log

CodePudding user response:

09-03 20:35:51. 745, 5876-5876/com. Example. Administrator. Myapplication2 E/AndroidRuntime: FATAL EXCEPTION: the main
Process: com. Example. Administrator myapplication2, PID: 5876
Java. Lang. NullPointerException: Attempt to invoke virtual method 'void Java. IO. OutputStream. Write (byte [])' on a null object reference
At com. Example. Administrator. Myapplication2. MainActivity $1. The onClick (MainActivity. Java: 36)
An android. View. The view. PerformClick (the Java: 4780)
An android. View. The view $PerformClick. Run (19866) view. Java:
An android. OS. Handler. HandleCallback (Handler. Java: 739)
An android. OS. Handler. DispatchMessage (Handler. Java: 95)
. An android OS. Stars. Loop (135). Which Java:
. An android app. ActivityThread. Main (5254) ActivityThread. Java:
The at Java. Lang. Reflect. Method. Invoke (Native Method)
The at Java. Lang. Reflect. Method. Invoke (372) Method. The Java:
At com. Android. Internal. OS. $MethodAndArgsCaller ZygoteInit. Run (ZygoteInit. Java: 903)
At com. Android. Internal. OS. ZygoteInit. Main (ZygoteInit. Java: 698)
09-03 20:35:51. 854, 406-2173/system_process E//EGL - ERROR: EGLBoolean __egl_platform_create_surface_window (egl_surface *, mali_base_ctx_handle) : 933:=========& gt; The usage of 0 x900
09-03 20:35:51. 872, 406-2173/system_process E//EGL - ERROR: EGLBoolean __egl_platform_create_surface_window (egl_surface *, mali_base_ctx_handle) : 933:=========& gt; The usage of 0 x900

CodePudding user response:

The thread is not started OutputSream is a null pointer
And don't have to open a thread to create a socket connection
When the write content can consider to use thread infinite loop reads

CodePudding user response:

E1 control without the data,
Out. Write (values. GetBytes ()); Null reference values is null because it is over, click the button to fill in some data in the EditText e1 before;
  • Related