ContentProvider: ManagedQuery() 有效;查询()失败
在我的应用程序中,我正在将数据库访问包装到 ContentProvider 中。这是因为有时我会因为两个线程同时访问数据库而崩溃。我严重依赖后台线程从 Web 服务器中提取数据并将其放入数据库中;当用户在活动之间切换时,您可能会同时运行多个活动。这就是原因。
现在说说如何。 我已经实现了 ContentProvider 类,并且看起来工作正常。在清单中注册,这也显示在 LogCat 中,表明提供程序处于活动状态。
该应用程序及其所有活动通过包装类访问数据库,该包装类本身不是活动。此类的构造函数将调用活动的上下文作为参数。那里没什么特别的。
现在我的问题是: 通过 context.query() 方法访问内容提供程序时,我遇到崩溃:
04-15 23:05:33.699: ERROR/AndroidRuntime(3564): Caused by: java.lang.NullPointerException
04-15 23:05:33.699: ERROR/AndroidRuntime(3564): at android.content.ContentResolver.acquireProvider(ContentResolver.java:727)
04-15 23:05:33.699: ERROR/AndroidRuntime(3564): at android.content.ContentResolver.query(ContentResolver.java:239)
04-15 23:05:33.699: ERROR/AndroidRuntime(3564): at squirrel.DeaddropDroid.DeaddropDB.query(DeaddropDB.java:482)
有问题的行是:
final Cursor blog2 = db.query(DeaddropDB.BLOG_TABLE, new String[] {
DeaddropDB.KEY_ID, DeaddropDB.KEY_DATE,
DeaddropDB.KEY_BLOG_SUMMARY }, null, null,
DeaddropDB.KEY_DATE + " DESC");
db 是一个数据库对象,构造函数将活动的上下文作为参数。 在同一活动中,以下行按预期工作并给出正确的结果:
final Cursor blog = managedQuery(DeaddropDBProvider.BLOG_URI,
new String[] {DeaddropDB.KEY_ID, DeaddropDB.KEY_DATE,
DeaddropDB.KEY_BLOG_SUMMARY }, null, null,
DeaddropDB.KEY_DATE + " DESC");
这证明我的 ContentProvider 可以正常工作并且已正确注册。我真的对这里发生的事情感到茫然!为什么这个查询不能像宣传的那样工作?
Within my app I'm in the process of wrapping the database access into a ContentProvider. This because occasionally I have crashes because two threads access the database at the same time. I rely heavily on background threads to pull down data from a web server and put that in the database; when the user switches between activities you may have multiple of those running at the same time. That's the why.
Now the how.
I've implemented the ContentProvider class, and that seems to work fine. Registered in the manifest, and this also shows up in LogCat that the provider is active.
The app, with all its activities, accesses the database through a wrapper class which itself is not an activity. The constructor of this class takes the context of the calling activity as parameter. Nothing special there.
Now my problem:
When accessing the content provider through the context.query() method, I get a crash:
04-15 23:05:33.699: ERROR/AndroidRuntime(3564): Caused by: java.lang.NullPointerException
04-15 23:05:33.699: ERROR/AndroidRuntime(3564): at android.content.ContentResolver.acquireProvider(ContentResolver.java:727)
04-15 23:05:33.699: ERROR/AndroidRuntime(3564): at android.content.ContentResolver.query(ContentResolver.java:239)
04-15 23:05:33.699: ERROR/AndroidRuntime(3564): at squirrel.DeaddropDroid.DeaddropDB.query(DeaddropDB.java:482)
The offending line is:
final Cursor blog2 = db.query(DeaddropDB.BLOG_TABLE, new String[] {
DeaddropDB.KEY_ID, DeaddropDB.KEY_DATE,
DeaddropDB.KEY_BLOG_SUMMARY }, null, null,
DeaddropDB.KEY_DATE + " DESC");
db is a DB object, the constructor takes the activity's context as parameter.
In the same activity, the following line works as expected and gives the correct results:
final Cursor blog = managedQuery(DeaddropDBProvider.BLOG_URI,
new String[] {DeaddropDB.KEY_ID, DeaddropDB.KEY_DATE,
DeaddropDB.KEY_BLOG_SUMMARY }, null, null,
DeaddropDB.KEY_DATE + " DESC");
This proves that my ContentProvider works and is registered properly. I'm really at a loss on what's going on here! Why does this query not work as advertised?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后,正如所承诺的,我的答案。
是的,所以这毕竟是我的错(当然)。对 Android 的源代码进行了一些挖掘(对开源的另一个赞),并通过一些进一步的测试,我找到了原因。我的 Uri 为空!我尝试从提供者导入 uri,但出现问题。
这将是我的下一个问题,因为虽然我有解决方法,但我不明白为什么会出错。
这是我的下一个问题:从 myContentProvider 加载 URI 失败(空结果),虽然我有一个可行的解决方案,但我没有不明白为什么会出错。有关该问题的更多详细信息参见此处。
And finally, as promised, my answer.
Right, so it was my fault after all (of course). Took some digging in Android's sources (another thumbs-up for open sources), and with some further testing I found the reason. My Uri was null! I try to import the uri from the provider but there something goes wrong.
That's going to be my next question, as while I have the workaround, I don't understand why it goes wrong.
That's my next question: Load URI from myContentProvider fails (null result), as while I have a working solution, I don't understand why it goes wrong. Further details on the issue are over there.