SecurityException: Failed to find provider null fo

2019-04-18 11:20发布

I have an app that is using ActiveAndroid and it's been working fine. However; now when I try to save a model to the database I'm getting a SecurityException.

The stack is:

Error saving model java.lang.SecurityException: Failed to find provider null for user 0; expected to find a valid ContentProvider for this authority 
at android.os.Parcel.readException(Parcel.java:1942) 
at android.os.Parcel.readException(Parcel.java:1888) 
at android.content.IContentService$Stub$Proxy.notifyChange(IContentService.java:801) 
at android.content.ContentResolver.notifyChange(ContentResolver.java:2046) 
at android.content.ContentResolver.notifyChange(ContentResolver.java:1997) 
at android.content.ContentResolver.notifyChange(ContentResolver.java:1967) 
at com.activeandroid.Model.save(Model.java:162)
[.... local stack removed]

Has anyone else experienced this? Do we need to specify the Content Provider in the AndroidManifest.xml?

Sorry but I do not have an isolated example of this yet. I will work to put something together.

Thanks in advance

9条回答
在下西门庆
2楼-- · 2019-04-18 11:48

i know there already good answer is there, but i dont understand how to best way to fix this bug and read this post Fixing SecurityException requiring a valid ContentProvider on Android 8, it very helpful to fix my bug i hope its helpful for other, Cheers!!!

查看更多
Rolldiameter
3楼-- · 2019-04-18 11:50

I was redirected to this question when I was searching for the same problem. However, this specific question concerns to an specific library implementation. To overcome this error due to the changes which are newly introduced in Android O, we have to provide the specific authority to the ContentProvider by adding the following in the AndroidManifest.xml file under application tag.

<provider
    android:name=".DatabaseContentProvider"
    android:authorities="com.yourpackage.name"
    android:exported="false" /> 

And you need to have a ContentProvider class like the following.

public class DatabaseContentProvider extends ContentProvider {

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        return null;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri,
                        @Nullable String[] projection,
                        @Nullable String selection,
                        @Nullable String[] selectionArgs,
                        @Nullable String sortOrder) {
        return null;
    }

    @Override
    public boolean onCreate() {
        return true;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values,
                      @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }
}
查看更多
爷的心禁止访问
4楼-- · 2019-04-18 11:51

I had the exact same problem with active android on Android O. Turns out that one of the methods in my custom ContentProvider returned a Uri, some times it would return null and this was causing the issue. So I added the @Nullable annotation to the method as shown below which fixed the problem.

@Nullable
@Override
public Uri insert(Uri uri, ContentValues contentValues) {..}
查看更多
登录 后发表回答