【问题标题】:SearchView stops the application when no result foundSearchView 在未找到结果时停止应用程序
【发布时间】:2019-01-29 06:36:28
【问题描述】:

我在每个屏幕的操作栏中都有一个搜索图标。 它确实找到了数据库中存在的项目,但是对于不存在的项目,它不会显示消息 not

找到了。相反,它的应用程序停止。我必须在清单文件中添加任何内容吗? php文件返回echo "0";如果没有找到结果。

可搜索的.xml

<?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;
...
}

【问题讨论】:

    标签: java android sql-server searchview


    【解决方案1】:

    可以尝试添加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();
           }
    

    【讨论】:

      猜你喜欢
      • 2016-11-27
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      相关资源
      最近更新 更多