判空处理上一般知道的方法有以下几种,我们测试下看看会遇到什么问题,执行结果又是如何呢?
.java
主要测试方法(测试数据 ""和 null)
1、String isEmpty()
2、TextUtils.isEmpty(CharSequence str)
3、!=null
public class MainActivity extends AppCompatActivity {
TextView clickTest, clickTest2, clickTest3, show, show2, show3;
static final int TYPE_01 = 1, TYPE_02 = 2, TYPE_03 = 3;
String needData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
needData = null;
show = findViewById(R.id.show);
show2 = findViewById(R.id.show2);
show3 = findViewById(R.id.show3);
clickTest = findViewById(R.id.dianji);
clickTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
doTest(needData, TYPE_01);
} catch (Exception e) {
show.setText("Exception01:" + e.getMessage());
Log.i("TAG", "捕捉到异常,类型" + TYPE_01 + ":" + e.getMessage());
e.printStackTrace();
}
}
});
clickTest2 = findViewById(R.id.dianji2);
clickTest2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
doTest(needData, TYPE_02);
} catch (Exception e) {
show2.setText("Exception02:" + e.getMessage());
Log.i("TAG", "捕捉到异常,类型" + TYPE_02 + ":" + e.getMessage());
e.printStackTrace();
}
}
});
clickTest3 = findViewById(R.id.dianji3);
clickTest3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
doTest(needData, TYPE_03);
} catch (Exception e) {
show3.setText("Exception03:" + e.getMessage());
Log.i("TAG", "捕捉到异常,类型" + TYPE_03 + ":" + e.getMessage());
e.printStackTrace();
}
}
});
}
/**
* 判空
*/
protected void doTest(String str, int type) throws Exception {
if (type == TYPE_01) {
if (str.isEmpty()) {
throw new Exception("测试数据" + "str-->\n" + str + "\n" + "String Method isEmpty()情况下 数据为空");
} else {
Toast.makeText(MainActivity.this, "显示有效数据", Toast.LENGTH_SHORT).show();
}
} else if (type == TYPE_02) {
if (TextUtils.isEmpty(str)) {
throw new Exception("测试数据" + "str-->\n" + str + "\n" + "TextUtils Method isEmpty(CharSequence str)情况下 数据为空");
} else {
Toast.makeText(MainActivity.this, "显示有效数据", Toast.LENGTH_SHORT).show();
}
} else {
if (str != null) {
throw new Exception("测试数据" + "str-->\n" + str + "\n" + "String Method != null 情况下 数据为空");
} else {
Toast.makeText(MainActivity.this, "显示有效数据", Toast.LENGTH_SHORT).show();
}
}
}
}
.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f4f5f7"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/dianji"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:clickable="true"
android:gravity="center"
android:text="测试情况1(单击)"
android:textColor="#232323"
android:textSize="18dp" />
<TextView
android:id="@+id/dianji2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:clickable="true"
android:gravity="center"
android:text="测试情况2(单击)"
android:textColor="#232323"
android:textSize="18dp" />
<TextView
android:id="@+id/dianji3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:clickable="true"
android:gravity="center"
android:text="测试情况3(单击)"
android:textColor="#232323"
android:textSize="18dp" />
<TextView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:clickable="true"
android:textColor="#d91c1a"
android:textSize="16dp" />
<TextView
android:id="@+id/show2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:clickable="true"
android:textColor="#d91c1a"
android:textSize="16dp" />
<TextView
android:id="@+id/show3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:clickable="true"
android:textColor="#d91c1a"
android:textSize="16dp" />
</LinearLayout>
测试结果
null 情况下:

分析String isEmpty() 抛了异常 提示方法不存在 我查看了一下它的方法源码
/**
* Returns {@code true} if, and only if, {@link #length()} is {@code 0}.
*
* @return {@code true} if {@link #length()} is {@code 0}, otherwise
* {@code false}
*
* @since 1.6
*/
public boolean isEmpty() {
// Android-changed: Get length from count field rather than value array (see above).
// Empty string has {@code count == 0} with or without string compression enabled.
// return value.length == 0;
return count == 0;
}
关键就是一句 return count == 0;测试数据本来就是null 所有抛了异常
27




被折叠的 条评论
为什么被折叠?



