我的问题与这个问题非常相似,但我还没有创建任何输出流
// Try to connect
try {
// Check the network state
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
// If there is conection, try to create a socket with remote server
if (networkInfo.isConnected()) {
InetAddress address = InetAddress
.getByName(serverAdressTextField.getText().toString());
Socket serverConnection = new Socket(address, PORT);
// Write message of success
//TODO Implement the rest of login function
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
} else {
// Say user that there is no internet
Toast.makeText(this, "No connection", Toast.LENGTH_SHORT)
.show();
}
// Catch unknown host and prompt error
} catch (UnknownHostException e) {
Toast.makeText(this, "Server is not reached", Toast.LENGTH_SHORT)
.show();
return;
// Prompt IO exception
} catch (IOException e) {
Toast.makeText(this, "I/O Error", Toast.LENGTH_SHORT).show();
return;
// Any other exception, not good
} catch (Exception e) {
Toast.makeText(this, "Unknown Error", Toast.LENGTH_SHORT).show();
return;
}
它抛出 IllegalStateException 并且如果没有被捕获,它会给出
02-17 05:29:59.655: E/AndroidRuntime(18470): FATAL EXCEPTION: main
02-17 05:29:59.655: E/AndroidRuntime(18470): java.lang.IllegalStateException: Could not execute method of the activity
02-17 05:29:59.655: E/AndroidRuntime(18470): at android.view.View$1.onClick(View.java:3591)
02-17 05:29:59.655: E/AndroidRuntime(18470): at android.view.View.performClick(View.java:4084)
02-17 05:29:59.655: E/AndroidRuntime(18470): at android.view.View$PerformClick.run(View.java:16966)
02-17 05:29:59.655: E/AndroidRuntime(18470): at android.os.Handler.handleCallback(Handler.java:615)
02-17 05:29:59.655: E/AndroidRuntime(18470): at android.os.Handler.dispatchMessage(Handler.java:92)
02-17 05:29:59.655: E/AndroidRuntime(18470): at android.os.Looper.loop(Looper.java:137)
02-17 05:29:59.655: E/AndroidRuntime(18470): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-17 05:29:59.655: E/AndroidRuntime(18470): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 05:29:59.655: E/AndroidRuntime(18470): at java.lang.reflect.Method.invoke(Method.java:511)
02-17 05:29:59.655: E/AndroidRuntime(18470): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-17 05:29:59.655: E/AndroidRuntime(18470): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-17 05:29:59.655: E/AndroidRuntime(18470): at dalvik.system.NativeStart.main(Native Method)
02-17 05:29:59.655: E/AndroidRuntime(18470): Caused by: java.lang.reflect.InvocationTargetException
02-17 05:29:59.655: E/AndroidRuntime(18470): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 05:29:59.655: E/AndroidRuntime(18470): at java.lang.reflect.Method.invoke(Method.java:511)
02-17 05:29:59.655: E/AndroidRuntime(18470): at android.view.View$1.onClick(View.java:3586)
02-17 05:29:59.655: E/AndroidRuntime(18470): ... 11 more
02-17 05:29:59.655: E/AndroidRuntime(18470): Caused by: android.os.NetworkOnMainThreadException
02-17 05:29:59.655: E/AndroidRuntime(18470): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
02-17 05:29:59.655: E/AndroidRuntime(18470): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
02-17 05:29:59.655: E/AndroidRuntime(18470): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
02-17 05:29:59.655: E/AndroidRuntime(18470): at libcore.io.IoBridge.connect(IoBridge.java:112)
02-17 05:29:59.655: E/AndroidRuntime(18470): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
02-17 05:29:59.655: E/AndroidRuntime(18470): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
02-17 05:29:59.655: E/AndroidRuntime(18470): at java.net.Socket.startupSocket(Socket.java:566)
02-17 05:29:59.655: E/AndroidRuntime(18470): at java.net.Socket.<init>(Socket.java:225)
02-17 05:29:59.655: E/AndroidRuntime(18470): at com.snivik.morze.LoginWindow.connectToServer(LoginWindow.java:62)
02-17 05:29:59.655: E/AndroidRuntime(18470): ... 14 more