是什么原因导致Android的ContentResolver.query()返回空值? [英] What causes Android's ContentResolver.query() to return null?

查看:850
本文介绍了是什么原因导致Android的ContentResolver.query()返回空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在什么情况下<一href="http://developer.android.com/reference/android/content/ContentResolver.html">ContentResolver.query()返回游标对象的空呢?我已经得到了空指针之前,但只是意识到该方法还可以返回null。我一直无法追查的情况下,出现这种情况的,虽然。

Under what conditions does ContentResolver.query() return null instead of a cursor object? I've gotten empty cursors before but just realized that the method can also return null. I haven't been able to trace the circumstances that this happens in, though.

推荐答案

我只是绊了同样的问题,因为我今天收到了我的一个应用程序,用户崩溃报告。如果Android的文档是不清楚的东西它有助于查看源$ C ​​$ C。这是我发现对原因 ContentResolver.query()返回

I just stumbled over the same problem due to a user crash report I received today for an app of mine. If the Android documentation is unclear about something it helps looking at the source code. This is what I found about the causes for ContentResolver.query() returning null:

  1. 内容提供者,不能获得的。这可能是由于与指定的乌里一个问题,或者是因为它只是在系统上不存在。如果URI是问题的原因是:协议不是内容:// 或开放的我们没有一个权威的字符串部分(Uri.getAuthority()== NULL)

  1. The content provider cannot be acquired. This can be due to a problem with the specified Uri or because it simply does not exist on the system. If the Uri is the problem the causes are: protocol is not content:// or the Uri does not have an authority string portion (Uri.getAuthority() == null).

该收购商的<一个href="http://developer.android.com/reference/android/content/ContentProvider.html#query%28android.net.Uri,%20java.lang.String%5B%5D,%20java.lang.String,%20java.lang.String%5B%5D,%20java.lang.String%29">query方法本身返回

可以获取的内容供应商,而是一个的RemoteException 被扔在查询过程。

The content provider could be acquired but a RemoteException was thrown during a query.

特别是由于(2)它是pretty的太多任意可能是什么原因,结果因为没有定义的规则。但通常情况下,如果SQLite是的后端一个的ContentProvider 你可以期望至少有一些空的Cursor对象作为一个结果,而不是仅仅

Especially because of (2.) it's pretty much arbitrary what might be the cause for null as a result since there are no rules defined. But usually, if SQLite is the back-end of a ContentProvider you can expect at least some empty Cursor object as a result instead of just null.

Android系统的ContentProvider 取值做一些检查,虽然才返回任何东西。如果输入未如预期有不太可能的机会,可以返回。但说实话,这从来没有发生在我身上了。我通常会得到一个抛出:IllegalArgumentException 的情况下,查询参数的问题。也许有些ContentProvider的实现返回的情况下,空的结果集。

Android system ContentProviders do some checks though before they return anything. If the input is not as expected there's the unlikely chance that null may be returned. But to be honest, that never happened to me before. I usually get an IllegalArgumentException in case of query parameter problems. Maybe some ContentProvider implementations return null in case of empty result sets.

无论哪种方式。这似乎是必要的总是检查空。特别是一些原因(3)可能的东西,可以在任何Android设备发生。

Either way. It seems to be necessary to always check for null. Especially reason number (3.) is probably something that can happen on any Android device.

这篇关于是什么原因导致Android的ContentResolver.query()返回空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆