一,数据类型转换,很多已经发布的应用,因为平时开发中没有考虑到一些基本的数据类型,导致应用crash掉。Object类型的对象,substring函数。
(1)object 类型对想,对其直接字符串操作函数toString,当其为null时就会crash
int result = Integer.valueOf(obj.toString); 如果obj为空,那么就会crash
所以这里需要对obj进行转换:
public final static int converToInt(Obkect obj, int defaultValue){
if(obj == null || "".equals(obj.toString.trim())){
return defaultValue;
}
try{
return Integer.ValueOf(obj.toString);
}catch(Exception e){
try{
return Doule.ValueOf(obj.toString).intValue();
}catch(Exception e){
return defaultValue;
}
}
}
(2),Java 中的substring函数 如果截取的字段长度不够则会crash掉。
String testStr ="T";
String subTestStr =testStr.sbustring(1,2); 这样则会报错,程序将会crash掉
String testStr ="T";
String subTestStr =“”;
if(testStr.length()>1){
testStr.sbustring(1,2)
}