我的 Android ContentProvider 找不到 ContentResolver

发布于 2024-11-10 09:28:22 字数 2943 浏览 4 评论 0原文

我的目标是编写一个没有 Activity 的 ContentProvider。为了进行测试,我在自己的应用程序中编写了一个测试活动。对于那些想告诉我 Android 中仍然包含记录器的人,我知道这一点。

这是 ContentProvider 的一部分

public static final String AUTHORITY = "de.xyz.android.log4android";
public static final int LOGGER_ARROW_URI_ID = 1;
public static final String ARROW_CONTENT_TYPE = 
    "vnd.de.xyz.android.cursor.dir/vnr.log";

public static final int LOGGER_ITEM_URI_ID = 2;
public static final String ITEM_CONTENT_TYPE = 
    "vnd.de.xyz.android.cursor.item/vnr.log";

public static final UriMatcher sUriMatcher;
static {
    sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    sUriMatcher.addURI(AUTHORITY, LOGGER_TABLE, LOGGER_ARROW_URI_ID);
    sUriMatcher.addURI(AUTHORITY, LOGGER_TABLE + "#", LOGGER_ITEM_URI_ID);
}

public static final Uri CONTENT_URI = Uri.parse("content://"
        + LogServiceProvider.AUTHORITY + "/logs");

public static final DefaultLogEntry LOG_ENTRY = null;

//Some more not important code

@Override
public Uri insert(Uri uri, ContentValues initialValues) {
    synchronized (this) {
        try {
            long rowID = dbHelper.getWritableDatabase().insert(LOGGER_TABLE,
                    null, initialValues);

            if (rowID > 0) {
                Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
                getContext().getContentResolver().notifyChange(_uri, null);
                return _uri;
            }
        } catch (Exception ex) {
        }
        throw new SQLException("Failed to insert row into " + uri);
    }
}

//Some more not important code

,所以我还将内容提供程序信息添加到 manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="de.xyz.android.logger"
          android:versionCode="1"
          android:versionName="1.0">

    <provider 
        android:name="de.xyz.android.logger.LogServiceProvider" 
        android:authorities="de.xyz.android.log4android"/>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <uses-sdk android:minSdkVersion="4" /> 
</application>

这是我的客户端活动的一部分,位于单独的应用程序中。

ContentResolver cr = getContentResolver();
Uri getItem = Uri.parse("content://de.xyz.android.log4android/logs");
ContentValues values = new ContentValues();
values.put("level","WARNING");
values.put("time","1986-11-16");
values.put("message","FistTest");
values.put("owner","jsb");
values.put("file","testLog.java");
values.put("line","27");
Uri newItem = cr.insert(getItem, values);

LogCat 告诉我 TestClient 找不到 ContentProvider

05-27 12:42:52.099: 错误/ActivityThread(548): 无法找到 de.xyz.android.log4android 的提供者信息

我的错误在哪里。在我看来,我做了所有提到的:内容提供商 |安卓。如果您能给我一个提示,那就太好了。如果您需要更多代码片段来理解,请询问我。

My goal is writing a ContentProvider without an Activity. For testing I wrote a test - activity in a own app. For those who want to tell me that there is still a logger included in android, I know this.

Here is part of the ContentProvider

public static final String AUTHORITY = "de.xyz.android.log4android";
public static final int LOGGER_ARROW_URI_ID = 1;
public static final String ARROW_CONTENT_TYPE = 
    "vnd.de.xyz.android.cursor.dir/vnr.log";

public static final int LOGGER_ITEM_URI_ID = 2;
public static final String ITEM_CONTENT_TYPE = 
    "vnd.de.xyz.android.cursor.item/vnr.log";

public static final UriMatcher sUriMatcher;
static {
    sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    sUriMatcher.addURI(AUTHORITY, LOGGER_TABLE, LOGGER_ARROW_URI_ID);
    sUriMatcher.addURI(AUTHORITY, LOGGER_TABLE + "#", LOGGER_ITEM_URI_ID);
}

public static final Uri CONTENT_URI = Uri.parse("content://"
        + LogServiceProvider.AUTHORITY + "/logs");

public static final DefaultLogEntry LOG_ENTRY = null;

//Some more not important code

@Override
public Uri insert(Uri uri, ContentValues initialValues) {
    synchronized (this) {
        try {
            long rowID = dbHelper.getWritableDatabase().insert(LOGGER_TABLE,
                    null, initialValues);

            if (rowID > 0) {
                Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
                getContext().getContentResolver().notifyChange(_uri, null);
                return _uri;
            }
        } catch (Exception ex) {
        }
        throw new SQLException("Failed to insert row into " + uri);
    }
}

//Some more not important code

So I also add the Content Provider Information to manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="de.xyz.android.logger"
          android:versionCode="1"
          android:versionName="1.0">

    <provider 
        android:name="de.xyz.android.logger.LogServiceProvider" 
        android:authorities="de.xyz.android.log4android"/>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <uses-sdk android:minSdkVersion="4" /> 
</application>

Here Is the part out of my Client activity, that is in a seperate app.

ContentResolver cr = getContentResolver();
Uri getItem = Uri.parse("content://de.xyz.android.log4android/logs");
ContentValues values = new ContentValues();
values.put("level","WARNING");
values.put("time","1986-11-16");
values.put("message","FistTest");
values.put("owner","jsb");
values.put("file","testLog.java");
values.put("line","27");
Uri newItem = cr.insert(getItem, values);

LogCat tells me that TestClient cant find the ContentProvider

05-27 12:42:52.099: ERROR/ActivityThread(548): Failed to find provider info for de.xyz.android.log4android

Where is my error. In my opinion I did all mentioned in: Content Providers | Android. It would be nice if you could give me an hint. If you need more code fragments to understand ask me.

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

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

发布评论

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

评论(2

吻安 2024-11-17 09:28:22
<provider android:name=".LogServiceProvider"
          android:authorities="de.xyz.android.log4android"
          android:exported="true" />

应该有帮助:)

<provider android:name=".LogServiceProvider"
          android:authorities="de.xyz.android.log4android"
          android:exported="true" />

should help :)

蓝咒 2024-11-17 09:28:22

一种可能失败的深奥方式是,如果您在内容提供程序上定义了一个带有参数的构造函数。这将导致默认的 0 参数构造函数不存在。这会导致提供程序的运行时注册失败,因为系统无法在清单中的 android:name 下指定的类中找到零参数构造函数。

One esoteric way in which this can fail is if you have a constructor defined on your content provider that takes an argument. That will cause the default 0 argument constructor to not exist. This causes the runtime registration of the provider to fail because the system cannot find a zero argument constructor in the class you specified under android:name in the manifest.

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