android – ParseQuery查找对象null

我使用以下代码搜索我的数据浏览器.列是位置和天气,该类称为Nowcast.

如果我的数据浏览器中有纽约并且天气“晴朗”,TextView showWeather会显示“纽约的天气晴朗”.

但是,如果我搜索洛杉矶并且它不在数据浏览器中,则显示“洛杉矶的天气为空”,而根据代码它根本不应输入if条件.我究竟做错了什么?

public void getData(){
    searchText = searchTextView.getText().toString();
    ParseQuery<ParseObject> query = ParseQuery.getQuery("Nowcast");
    query.whereEqualTo("Location", searchText);
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> object, ParseException e) {
            if (e == null) {
                for (ParseObject weatherObject : object) {
                    weatherData = weatherObject.getString("Weather");
                }
                showWeather.setText("The weather in " + searchTextToString + " is " + weatherData);
            } else {
                Toast.makeText(NowcastSearch.this, "No such location found",Toast.LENGTH_LONG).show();
            }
        }
    });
}

最佳答案

whereas it should not enter the if condition at all according to the code

仅当ParseException不为null时才应输入.换句话说,只有当您的查询出现问题时(例如,您为字段条件传递了错误的参数类型),它才应该输入if条件(它的第一个分支).

您的查询没有问题.所以没有抛出异常.不是单个记录与您的条件匹配的查询仍然是有效查询.对象列表只是空的,但整个过程没有任何错误.

点赞