[英]SearchView stops the application when no result found
我在每个屏幕的操作栏中都有一个搜索图标。 它确实找到了数据库中存在的项目,但是对于不存在的项目,它不会显示消息 not
成立。 相反,它的应用程序停止。 我必须在清单文件中添加任何内容吗? php 文件返回 echo "0"; 如果没有找到结果。
可搜索文件
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint" />
每个类“实现 SearchView.OnQueryTextListener {”并在其上添加此代码。
...
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
String name=query;
new Server(getBaseContext(),2,name.execute();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
...
以及服务器类中的特定部分
protected String communicate(String argument1 ){
.....
case 2:
link="...php";
this.query=name;
break;
.......
}
protected void exec(String list){
...
case 2:
if (!found.equals("0")) {
String[] list = found.split(",");
String check="";
for (int i=0;i<list.length;i++) {
if (query.equals(list[i].toLowerCase())) check=list[i];
}
String name=check;
new Server(context,3,name).execute();
}
else Toast.makeText(context, "Not found", Toast.LENGTH_LONG).show();
break;
...
}
可以尝试添加try/catch
结构吗?
case 2:
try {
if (!found.equals("0")) {
String[] list = found.split(",");
String check="";
for (int i=0;i<list.length;i++) {
if (query.equals(list[i].toLowerCase())) check=list[i];
}
String name=check;
new Server(context,3,name).execute();
}
} catch (Exception e) {
Toast.makeText(context, "Not found", Toast.LENGTH_LONG).show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.