【问题标题】:Failed to find provider info error未能找到提供商信息错误
【发布时间】:2015-04-24 02:00:36
【问题描述】:

我正在尝试使用 contentprovider ,但我遇到了问题,因为我收到“无法找到 com.example.alex.hopefulyworks 的提供者信息”的错误

这是清单和内容提供者

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alex.hopefulythisworks" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider android:name=".ColorContentProvider"
            android:authorities="com.example.alex.hopefulythisworks.ColorContentProvider"
            android:enabled="true"
            android:exported="true" >
        </provider>

    </application>

</manifest>





    package com.example.alex.hopefulythisworks;
     ....
    public class ColorContentProvider extends ContentProvider {

    private ColorHelper database;

    private static final String AUTHORITY
            = "com.example.alex.hopefulythisworks";

    private static final String BASE_PATH
            = "tasks";

    public static final Uri CONTENT_URI
            = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);
    public static final String CONTENT_URI_PREFIX
            = "content://" + AUTHORITY + "/" + BASE_PATH + "/";

    private static final UriMatcher sURIMatcher =
            new UriMatcher(UriMatcher.NO_MATCH);

    private static final int COLOR = 1;
    private static final int COLOR_ROW = 2;

    static {
        sURIMatcher.addURI(AUTHORITY, BASE_PATH, COLOR);
        sURIMatcher.addURI(AUTHORITY, BASE_PATH + "/#", COLOR_ROW);
    }



    @Override
    public boolean onCreate() {
        database = new ColorHelper(getContext());
       Log.d("contentprovider" , "inside content provider oncreate ");
        return false;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int uriType = sURIMatcher.match(uri);

        SQLiteDatabase sqlDB = database.getWritableDatabase();

        long id = 0;
        switch (uriType) {
            case COLOR:
                id = sqlDB.insert(ColorTable.TABLE_TASK,
                        null, values);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: "
                        + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return Uri.parse( CONTENT_URI_PREFIX + id);
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {

        // check if the caller has requested a column which does not exists
        ColorTable.validateProjection( projection );

        // Using SQLiteQueryBuilder instead of query() method
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

        queryBuilder.setTables( ColorTable.TABLE_TASK );

        switch ( sURIMatcher.match(uri) ) {
            case COLOR:
                break;
            case COLOR_ROW:
                // add the task ID to the original query
                queryBuilder.appendWhere( ColorTable.COLUMN_ID + "=" + uri.getLastPathSegment() );
                break;
            default:
                throw new IllegalArgumentException("Invalid URI: " + uri);
        }

        System.out.println("before null check");
        if(database == null){
            System.out.println("database is null");
            database = new ColorHelper(getContext());
        }
        System.out.println("after null check");

        SQLiteDatabase db = database.getWritableDatabase();
        Cursor cursor = queryBuilder.query( db, projection, selection,
                selectionArgs, null, null, sortOrder);

        // notify listeners
        cursor.setNotificationUri(getContext().getContentResolver(), uri);

        return cursor;
    }


    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase sqlDB = database.getWritableDatabase();
        int rowsDeleted = 0;
        switch ( sURIMatcher.match(uri) ) {
            case COLOR:
                rowsDeleted = sqlDB.delete(ColorTable.TABLE_TASK, selection, selectionArgs);
                break;
            case COLOR_ROW:
                String id = uri.getLastPathSegment();
                if (TextUtils.isEmpty(selection)) {
                    rowsDeleted = sqlDB.delete( ColorTable.TABLE_TASK,
                            ColorTable.COLUMN_ID + "=" + id,
                            null);
                } else {
                    rowsDeleted = sqlDB.delete( ColorTable.TABLE_TASK,
                            ColorTable.COLUMN_ID + "=" + id
                                    + " and " + selection,
                            selectionArgs);
                }
                break;
            default:
                throw new IllegalArgumentException("Invalid URI: " + uri);
        }

        getContext().getContentResolver().notifyChange(uri, null);
        return rowsDeleted;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection , String[] selectionArgs) {


        SQLiteDatabase sqlDB = database.getWritableDatabase();
        int rowsUpdated = 0;
        switch ( sURIMatcher.match(uri) ) {
            case COLOR:
                rowsUpdated = sqlDB.update( ColorTable.TABLE_TASK,
                        values,
                        selection,
                        selectionArgs);
                break;
            case COLOR_ROW:
                String id = uri.getLastPathSegment();
                if (TextUtils.isEmpty(selection)) {
                    rowsUpdated = sqlDB.update( ColorTable.TABLE_TASK,
                            values,
                            ColorTable .COLUMN_ID + "=" + id,
                            null );
                } else {
                    rowsUpdated = sqlDB.update( ColorTable.TABLE_TASK,
                            values,
                            ColorTable.COLUMN_ID + "=" + id
                                    + " and " + selection,
                            selectionArgs);
                }
                break;
            default:
                throw new IllegalArgumentException("Invalid URI: " + uri);
        }

        getContext().getContentResolver().notifyChange(uri, null);
        return rowsUpdated;
    }





}

【问题讨论】:

    标签: android android-contentprovider android-contentresolver


    【解决方案1】:

    authorities只需要包名,name是完整的包名和类名。

        <provider android:name="com.example.alex.hopefulythisworks.ColorContentProvider"
            android:authorities="com.example.alex.hopefulythisworks"
            android:enabled="true"
            android:exported="true" >
        </provider>
    

    请记住,这需要与您用于构建 Uri 的相同:

     private static final String AUTHORITY = "com.example.alex.hopefulythisworks";
    

    Android docs

    【讨论】:

      【解决方案2】:
      android:authorities="com.example.alex.hopefulythisworks.ColorContentProvider"
      

      应该和

      一样
      private static final String AUTHORITY
              = "com.example.alex.hopefulythisworks";
      

      所以另一种解决方案是将AUTHORITY 替换为

      private static final String AUTHORITY
              = "com.example.alex.hopefulythisworks.ColorContentProvider";
      

      希望对你有帮助。

      【讨论】:

        【解决方案3】:

        只需确保您在 searchable 以及在清单文件中声明的提供程序中使用完全限定名称为 authority,如下所示:

        <?xml version="1.0" encoding="utf-8"?>
        <searchable xmlns:android="http://schemas.android.com/apk/res/android"
            android:hint="@string/action_search"
            android:label="@string/app_name"
            android:searchSuggestAuthority="your_pakcage_name.SearchSuggestionProvider"
            android:searchSuggestSelection=" ?" />
        
        
        <provider
            android:authorities="your_pakcage_name.SearchSuggestionProvider"
            android:name="your_pakcage_name.SearchSuggestionProvider"
            android:exported="true"
            android:enabled="true"
            android:multiprocess="true"/>
        

        如果您的提供商中有敏感信息,请确保导出的不是 true

        【讨论】:

          【解决方案4】:
          <provider      
                android:name="com.example.absolutelysaurabh.todoapp.ColorContentProvider"
                android:authorities="com.example.absolutelysaurabh.todoapp"
                android:enabled="true"
                android:exported="false" >
          </provider>
          

          重要提示:确保在“合同”类中,您也使用了与此处相同的权限,即“com.example.absolutelysaurabh.todoapp”

          【讨论】:

          • 欢迎来到 SO。请阅读此how-to-answer 以提供高质量的答案。您的回复似乎重复了一个没有新信息的已接受答案。
          【解决方案5】:

          已解决

          我的AddStickerPackActivity.java 在函数createIntentToAddStickerPack 中遇到了这个问题 看了一些教程后我在哪里评论了intent.putExtra(StickerPackDetailsActivity.EXTRA_STICKER_PACK_AUTHORITY, BuildConfig.CONTENT_PROVIDER_AUTHORITY); 请检查您是否也发现相同的问题 取消注释此行并确保您没有导入任何 BuildConfig 库,是否不需要导入任何 BuildConfig

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-02-26
            • 2013-03-27
            • 1970-01-01
            • 1970-01-01
            • 2012-09-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多