TextUtils.isEmpty(String)与String.isEmpty()与String==null的区别
时间: 2023-06-04 11:02:10 浏览: 84
TextUtils.isEmpty(String)与String.isEmpty()是判断字符串是否为空的方法,而String==null则是判断字符串对象是否为null。TextUtils.isEmpty()方法可以避免空指针异常,因为它会先判断字符串对象是否为null,再判断字符串长度是否为0。而String.isEmpty()方法只判断字符串长度是否为0,如果字符串对象为null,则会抛出空指针异常。因此,TextUtils.isEmpty()方法更加严谨,建议使用。
相关问题
if (TextUtils,isEmpty(content)) { create le.createqecode return null; ivity eQRCodeBitmap(Strin // 宽和高>=0 eate(Bundle):void 55 if (width <0 ll height < 0) { Button return null; 家成二地 eView:lmageView } EditText try { 58 /**1.设置二维码相关配置 */ Hashtable<EncodeHintType, String> hints = new Hashtable<>(); v24 cher background.xml // 字符转码格式设置 cher _foreground.xmi if(!TextUtils.isEmpty(character_set)) { 65 hints,put(EncodeHintType.CHARACTER_SET, character_set); y_main.xmi } anydpi-v26 // 容错率设置 ncher.xml if (!TextUtils.isEmpty(error_correction_level)) { ncher round.xml 67 hints,put(EncodeHintType.ERROR_CORRECTION, error_correction_level); -hdpi qw e uncher.png // 空白边距设置 a s p-mdpi uncher_round.png 70 1f (!TextUtils.isEmpty(margin)) { 71 hints.put(EncodeHintType.MARGIN, margin); uncher.png uncher_round.png 72 p-xhdpi 2.将配置参数传入到QRCodeWriter的encode方法生成BitMatrix(位矩阵)对象 */ 7123 .737h p-schdpi BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.qr_cODE, W1C
这部分代码实现了生成二维码的具体逻辑。首先,代码通过 TextUtils.isEmpty() 方法判断传入的参数 content 是否为空,如果为空,则返回 null。接着,代码判断传入的宽度和高度是否小于 0,如果是,则返回 null。然后,代码创建了一个 Hashtable 对象 hints,用于存储二维码相关的配置参数。其中,通过 hints.put() 方法将字符转码格式、容错率和空白边距等参数设置到 hints 对象中。最后,代码调用 QRCodeWriter 的 encode() 方法生成了 BitMatrix 对象,这个对象是一个位矩阵,用于表示二维码的黑白像素矩阵。生成 BitMatrix 对象时需要传入三个参数:content、BarcodeFormat 和宽度高度。其中,BarcodeFormat.qr_code 表示生成二维码,宽度和高度表示生成的二维码图片的尺寸。
下面的函数是从 /sys下面读取某个文件的内容,请指出其中的错误 public static String readSysClassFile(String sys_path) { String prop = ""; BufferedReaderreader = null; try{ reader = new BufferedReader(new FileReader(sys_path)); prop = reader.readline(); if (prop != null && pro.length() > 0){ #Substring the prop prop = pro.Substring(0,15); } } } catch (IOException e){ e.printStackTrace(); }finally { if (reader != null){ try { reader.close(); }carch (IOException e){ e.printStackTrace(); } } } if (TextUtils.isEmpty(prop)){ prop= "NULL" } return prop; }
这段代码中有以下错误:
1. `BufferedReaderreader = null;` 应该为 `BufferedReader reader = null;`
2. `prop = reader.readline();` 应该为 `prop = reader.readLine();`
3. `if (prop != null && pro.length() > 0){` 应该为 `if (prop != null && prop.length() > 0){`
4. `#Substring the prop` 这一行应该是注释,应该改为 `// Substring the prop`
5. `}carch (IOException e){` 应该为 `}catch (IOException e){`
6. `if (TextUtils.isEmpty(prop)){` 前面没有导入 `TextUtils` 类,需要加上 `import android.text.TextUtils;`
修改后的代码如下:
```
import android.text.TextUtils;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public static String readSysClassFile(String sys_path) {
String prop = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(sys_path));
prop = reader.readLine();
if (prop != null && prop.length() > 0) {
// Substring the prop
prop = prop.substring(0, 15);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (TextUtils.isEmpty(prop)) {
prop = "NULL";
}
return prop;
}
```