java.lang.NumberFormatException: Invalid int: ""
2015-10-22 16:05
537 查看
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();
相关文章推荐
- 使用 XStream 把 Java 对象序列化为 XML
- spring mvc 表单提交
- eclipse设置编码
- java.util.vector中的vector的详细用法
- Java 序列化static变量问题
- java基础 多线程 简单总结
- java线程四种状态
- 收集到的一些java方面的博客文章
- myeclipse构建用户库
- HashMap遍历性能对比
- 一个可以拖动指针设置hour,minute的钟表,开源框架TimePickerDialog改动eclipse project
- Java FTP上传文件
- Spring学习笔记之基础、IOC、DI(1)
- Spring学习笔记之基础、IOC、DI(1)
- Java中的流程控制语句
- 我对Java Serializable(序列化)的理解和总结
- Eclipse开发快捷键
- Eclipse 常用快捷键
- java如何将字符转换成‘16进制格式’的字符串编码
- java如何得到GET和POST请求URL和参数列表