从myContentProvider加载URI失败(空结果) [英] Load URI from myContentProvider fails (null result)

查看:209
本文介绍了从myContentProvider加载URI失败(空结果)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,也许穷标题,不知道如何来形容这一点。

Sorry for the maybe poor title, no idea how to describe this well.

我写我自己的ContentProvider叫DeaddropDBProvider,其中包括以下行来设置一些常量的URI来提供内容:

I have written my own ContentProvider called DeaddropDBProvider, which includes the following lines to set some constants for the URIs to the provided content:

public static final String PROVIDER_NAME = 
    "squirrel.deaddropdroid.deaddropdbprovider";
public static final Uri BLOG_URI = 
    Uri.parse("content://"+ PROVIDER_NAME + "/blog");

现在去这些URI,我访问他们两种不同的方式。一件作品,其他的失败,我不明白为什么。

Now to get to these URIs, I'm accessing them in two different ways. One works, the other fails, and I don't understand why.

有故障的方法:

方法1)直接调用。这工作正常(简称code):

Method 1): direct call. That works fine (abbreviated code):

public class DeaddropDB {
    public void getData(...) {
        Cursor cursor = context.getContentResolver().query(DeaddropDBProvider.BLOG_URI,
                columns, selection, selectionArgs, orderBy);
    }
}

方法2):进口URI恒定;然后拨打电话。这给出了一个NullPointerException异常的时刻,我尝试使用URI,因为URI仍然空。

Method 2): import URI as constant; then make the call. This gives a NullPointerException the moment I try to use that URI, as the URI is still null.

public class DeaddropDB {
    public static final Uri BLOG_URI = DeaddropDBProvider.BLOG_URI;
    public void getData(...) {
        Cursor cursor = context.getContentResolver().query(BLOG_URI,
                columns, selection, selectionArgs, orderBy);
    }
}

如何走到这第二种方法不起作用?为什么BLOG_URI空?

How come this second method does not work? Why is BLOG_URI null?

推荐答案

我看到这是在这一点上陈旧的问题,但因为它没有一个答案,我想我会扔在我的2美分...

I see this is a stale question at this point, but since it doesn't have an answer I thought I'd throw in my 2 cents...

我认为这是失败的,因为当DeaddropDB.BLOG_URI初始化你DeaddropDBProvider.BLOG_URI没有完全初始化。

I think it's failing because your DeaddropDBProvider.BLOG_URI isn't fully initialized when DeaddropDB.BLOG_URI is initialized.

我不是一个Java专家,但我知道,静态变量的定义,当你第一次访问一个类;但是,如果有引用类B,然后B类试图引用类A有A类初始化器,爪哇可能不能正确地处理这种情况,因此,如果DeaddropDBProvider试图读取从DeaddropDB任何初始化静态变量,有可能是冲突。

I'm not a Java expert, but I know that static variables are defined when you first access a class; BUT, if there are initializers in class A that reference class B, and then class B tries to reference class A, Java may not handle that case correctly, so if DeaddropDBProvider tries to read anything from DeaddropDB to initialize a static variable, there may be a conflict.

我的建议是创建两个其他类阅读DeaddropDBSettings类。这样,你就不会结束相互初始化冲突。

My recommendation would be to create a DeaddropDBSettings class that both other classes read. That way you don't end up with mutual initialization conflicts.

这篇关于从myContentProvider加载URI失败(空结果)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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