java.lang.NumberFormatException: Invalid int: ""

来源:互联网 发布:销售软件 编辑:程序博客网 时间:2024/05/17 22:30

java.lang.NumberFormatException: Invalid int: “”

http://stackoverflow.com/questions/24910757/java-lang-numberformatexception-invalid-int-null

错误代码

hostText = (EditText) findViewById(R.id.host);portText = (EditText) findViewById(R.id.port);String hostName = hostText.getText().toString().trim();String portString = hostText.getText().toString().trim();int port;if(portString==null){    port=3000;}else{    port=Integer.parseInt(portString);}           if (hostName == null) {    hostName = "192.168.13.245";}Log.d(tag, "Input host name is: "+hostName);SocketClient client = new SocketClient();client.host = hostName;client.port = port;client.start();         

正确代码

hostText = (EditText) findViewById(R.id.host);portText = (EditText) findViewById(R.id.port);String hostName = hostText.getText().toString().trim();String portString = hostText.getText().toString().trim();int port;try {    port = Integer.parseInt(portString);} catch (NumberFormatException e) {    port = 3000;}                   if (hostName == null) {        hostName = "192.168.13.245";        }    Log.d(tag, "Input host name is: "+hostName);    SocketClient client = new SocketClient();    client.host = hostName;    client.port = port;    client.start();         
0 0