2013-02-17 78 views
0

我的問題是很相同的,這一個,但我不會創建任何輸出流還Android的套接字致命錯誤

FATAL Exception Main android

// 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 

回答

3

你的問題是,你顯然試圖在主線程中打開一個套接字,這就是爲什麼你得到android.os.NetworkOnMainThreadException

所有網絡必須在後臺線程中完成。

+0

謝謝你,我在例外的第5行迷路了,並決定去問周圍的其他人。 – 2013-02-17 23:32:41