2012-10-02 102 views
1

我想在android設備上創建一個套接字連接。 但只要我寫Socket s = new Socket("192.168.1.126",3000)並運行它給我的錯誤作爲創建套接字時出錯?

FATAL EXCEPTION: main 
10-01 16:18:55.509: E/AndroidRuntime(15664): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.senddata1/com.example.senddata1.SendData1}: java.lang.NullPointerException: println needs a message 
10-01 16:18:55.509: E/AndroidRuntime(15664): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1818) 
10-01 16:18:55.509: E/AndroidRuntime(15664): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1834) 
10-01 16:18:55.509: E/AndroidRuntime(15664): at android.app.ActivityThread.access$500(ActivityThread.java:122) 
10-01 16:18:55.509: E/AndroidRuntime(15664): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1027) 
10-01 16:18:55.509: E/AndroidRuntime(15664): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-01 16:18:55.509: E/AndroidRuntime(15664): at android.os.Looper.loop(Looper.java:132) 
10-01 16:18:55.509: E/AndroidRuntime(15664): at android.app.ActivityThread.main(ActivityThread.java:4126) 
10-01 16:18:55.509: E/AndroidRuntime(15664): at java.lang.reflect.Method.invokeNative(Native Method) 
10-01 16:18:55.509: E/AndroidRuntime(15664): at java.lang.reflect.Method.invoke(Method.java:491) 
10-01 16:18:55.509: E/AndroidRuntime(15664): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844) 
10-01 16:18:55.509: E/AndroidRuntime(15664): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 
10-01 16:18:55.509: E/AndroidRuntime(15664): at dalvik.system.NativeStart.main(Native Method) 
10-01 16:18:55.509: E/AndroidRuntime(15664): Caused by: java.lang.NullPointerException: println needs a message 
10-01 16:18:55.509: E/AndroidRuntime(15664): at android.util.Log.println_native(Native Method) 
10-01 16:18:55.509: E/AndroidRuntime(15664): at android.util.Log.e(Log.java:230) 
10-01 16:18:55.509: E/AndroidRuntime(15664): at com.example.senddata1.SendData1.onCreate(SendData1.java:66) 
10-01 16:18:55.509: E/AndroidRuntime(15664): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050) 
10-01 16:18:55.509: E/AndroidRuntime(15664): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1782) 
10-01 16:18:55.509: E/AndroidRuntime(15664): ... 11 more 

請幫助我,我面對的問題。

+0

你應該閱讀文檔。我認爲你沒有正確使用它。 http://developer.android.com/reference/java/net/Socket.html#Socket(java.net.Proxy) – JoxTraex

+0

似乎你正在使用'Log.e()'打印空值。你究竟打印了什麼? –

回答

0

你不能直接通過IP地址做這樣this.You要投它,我的InetAddress類

private String ip="192.168.1.126"; 
InetAddress serveradd = InetAddress.getByName(ip); 
socket=new DatagramSocket(6000,serveradd); 

Tt的將創建UDP連接插座或者您可以使用

Socket s=new Socket(serveradd,3000) 
+0

是的,你可以.... –