创造与使用 Android 的 ContentProvider

发布于 2024-10-07 09:14:50 字数 2351 浏览 1 评论 0原文

当我调用 Android ContentProvider 时,出现以下异常:

java.lang.RuntimeException:无法 开始活动 ComponentInfo{de.harm.android.couchone/de.harm.android.couchone.CouchContactClient}: java.lang.IllegalArgumentException: 未知网址 内容://de.harm.android.couchone.provider/test2

这些是项目:

Android 使用所谓的 ContentResolverContentProvider 进行通信,后者又处理持久性功能 -访问数据库。

ContentProvider 使用唯一的 Uri 注册自身。 ContentResolver 使用此 Uri 调用 ContentProvider 并传递其他数据,例如 SQL 查询字符串和/或要保存的数据。

CouchOneProvider/AndroidManifest.xml 中,我有以下内容:

<provider android:authorities="de.harm.android.couchone.provider"
   android:name=".Provider" />

Provider 使用

static {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI(PROVIDER_NAME, DB_Name, URI_COLLECTION);
    uriMatcher.addURI(PROVIDER_NAME, DB_Name + "/#", URI_ENTITY);
}

public static boolean isCollectionUri(Uri uri) {
    return uriMatcher.match(uri) == URI_COLLECTION;
}

处理 ContentResolver 使用的 CONTENT_URI code> 调用 ContentProvider

  • 我是否缺少 AndroidManifest.xml 中任何一个的权限?
  • 我在 AndroidManifest.xml 中定义的权限是否错误?
  • CONTENT_URI 是否错误?

更新:

我有其他信息:

Logcat 说:

无法找到提供商信息 de.harm.android.couchone.provider

这应该是起点。但到目前为止我找不到任何解决方案。

ContentProvider 实现的完全限定类名是:

de.harm.android.couchone.Provider

AndroidManifext.xml 中,它被指定为权限,除了名称是小写的,但这应该没问题。

包名称之前已在 xml 文件中定义,因此“.Provider”也应该没问题。

从异常中可以看出,客户端调用:

content://de.harm.android.couchone.provider/test2

Logcats 的答案是:

无法找到 de.harm.android.couchone.provider 的提供商信息

我没有看到缺少什么,也许是 Eclipse 或模拟器问题?

我将提供程序安装为“将项目作为 Android 应用程序运行”。

When I call an Android ContentProvider I get the following exception:

java.lang.RuntimeException: Unable to
start activity
ComponentInfo{de.harm.android.couchone/de.harm.android.couchone.CouchContactClient}:
java.lang.IllegalArgumentException:
Unknown URL
content://de.harm.android.couchone.provider/test2

These are the projects:

Android uses the so-called ContentResolver to communicate with ContentProvider which in turn handles the persistence functionality - accessing the database.

The ContentProvider registers itself with a unique Uri. The ContentResolver calls the ContentProvider with this Uri and passes additional data, like a SQL query string and/or data to be saved.

In the CouchOneProvider/AndroidManifest.xml I have the following:

<provider android:authorities="de.harm.android.couchone.provider"
   android:name=".Provider" />

The Provider uses

static {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI(PROVIDER_NAME, DB_Name, URI_COLLECTION);
    uriMatcher.addURI(PROVIDER_NAME, DB_Name + "/#", URI_ENTITY);
}

and

public static boolean isCollectionUri(Uri uri) {
    return uriMatcher.match(uri) == URI_COLLECTION;
}

to process the CONTENT_URI used by the ContentResolver to call the ContentProvider:

  • Am I missing permissions in any of both AndroidManifest.xml?
  • Am I defining the authority in AndroidManifest.xml wrongly?
  • Is the CONTENT_URI wrong?

Update:

I have additional information:

Logcat says:

Failed to find provider info for
de.harm.android.couchone.provider

This should be the starting point. But so far I couldn't find any solution.

The fully qualified classname of the ContentProvider implementation is:

de.harm.android.couchone.Provider

In AndroidManifext.xml exactly this is specified as authority, except for the name being to lower case, but this should be fine.

The package name is defined previously in the xml file, so ".Provider" should be ok, too.

As to be seen in the exception, the client calls:

content://de.harm.android.couchone.provider/test2

Logcats answer is:

Failed to find provider info for de.harm.android.couchone.provider

I don't see what's missing, perhaps it's Eclipse or emulator problem?

I install the provider as "run project as Android application".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我做我的改变 2024-10-14 09:14:50

我已经解决了这个问题:

两个项目都有相同的包结构。我将 de.harm.android.couchone 更改为 de.harm.android.couchone.provider 和 de.harm.android.couchone.client 。

I have resolved this problem:

Both projects had the same package structure. I changed de.harm.android.couchone to de.harm.android.couchone.provider and de.harm.android.couchone.client.

随心而道 2024-10-14 09:14:50

我认为这个链接与问题主题相关。如何实现自定义内容提供程序。

I think this link is related to question topic. How to implement a custom content-provider.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文