[英]Switch statement calling all the cases in AsyncTask even if the variable is not matching
我用 switch 语句创建了一个 AsyncTask。这里调用了测试和医学案例。非常奇怪的行为
BackendAsync backendAsync = new BackendAsync();
backendAsync.execute("Test");
public class BackendAsync extends AsyncTask<String,Void,Void>{
@Override
protected Void doInBackground(String... strings) {
switch (strings[0]){
case "Test": {
Log.d("Test", "doInBackground: Called Test");
}
case "Medicine": {
Log.d("Medicine", "doInBackground: Called Medicine");
}
}
return null;
}
}
结果:
D/Test:doInBackground:称为测试 D/Medicine:doInBackground:称为医学
像这样使用它
case "Test": {
Log.d("Test", "doInBackground: Called Test");
break;
}
case "Medicine": {
Log.d("Medicine", "doInBackground: Called Medicine");
break;
}
BackendAsync backendAsync = new BackendAsync();
backendAsync.execute("Test");
public class BackendAsync extends AsyncTask<String,Void,Void>{
@Override
protected Void doInBackground(String... strings) {
switch (strings[0]){
case "Test": {
Log.d("Test", "doInBackground: Called Test");
}
break;
case "Medicine": {
Log.d("Medicine", "doInBackground: Called Medicine");
}
breakl
}
return null;
}
}
在每个案例之后添加中断
public class BackendAsync extends AsyncTask<String,Void,Void>{
@Override
protected Void doInBackground(String... strings) {
switch (strings[0]){
case "Test": {
Log.d("Test", "doInBackground: Called Test");
break;
}
case "Medicine": {
Log.d("Medicine", "doInBackground: Called Medicine");
break;
}
}
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.