Android provider无法获取,打开provider失败(Android12,Api31)

1.运行TestProvider的时候报了Failed to find provider info for com.example.contentprvider3.Provider.UserProvider的错误,后来我把这种问题的解决方法都试了一遍依然不行,数据库是有的,也存入了数据,但还是获取不了ContentProvieder.
2.ContentProvider的代码

package com.example.contentprvider3.Provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.contentprvider3.utils.Constants;
import com.example.contentprvider3.Databasehelper.UserDatabasehelper;

import java.net.URI;

import javax.xml.transform.Result;


public class UserProvider extends ContentProvider {
    private static final  String SCHEMA="content://";
    private static  final  String AUTHORITY="com.example.contentprvider3.UserProvider";
    private static final String TAG = "UserProvider";
    private static Uri uri_provider =Uri.parse(SCHEMA+AUTHORITY);

    private UserDatabasehelper muserDatabaseHelper = null;
    private static final  int User_Match_Code = 1;
    private static UriMatcher suriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        suriMatcher.addURI(AUTHORITY,null,User_Match_Code);
    }

    @Override
    public boolean onCreate() {
        muserDatabaseHelper = new UserDatabasehelper(getContext());
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
      //分配规则
        int result = suriMatcher.match(uri);
        Log.d(TAG,"RESULT-----"+ result);
        if (result==User_Match_Code){
            SQLiteDatabase db = muserDatabaseHelper.getReadableDatabase();
            Cursor cursor = db.query(Constants.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);

        }else{
            //不匹配规则
            throw new IllegalArgumentException("参数错误");
        }

        return null;
    }

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

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {

        int result = suriMatcher.match(uri);
        if(result==User_Match_Code) {
        SQLiteDatabase db = muserDatabaseHelper.getWritableDatabase();
            long id = db.insert(Constants.TABLE_NAME, null, values);
            Uri uriresult = Uri.parse("content://com.example.contentprvider3.UserProvider"+id);
            Log.d(TAG,"insert result ----"+id);
            getContext().getContentResolver().notifyChange(uriresult,null);
        return uriresult;
        }else {
            throw new IllegalArgumentException();
        }
    }

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

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

3.ContentProvider的Manifest文件代码

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ContentPrvider3">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider
            android:exported="true"
            android:authorities="com.example.contentprvider3.UserProvider"
            android:grantUriPermissions="true"
            android:name=".Provider.UserProvider">

        </provider>


4.TestProvider部分的代码


package com.example.testprovider;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {
 private static final  String SCHEMA="content://";
 private static  final  String AUTHORITY="com.example.contentprvider3.Provider.UserProvider";
 private static  Uri uri_provider =Uri.parse(SCHEMA+AUTHORITY);
    private static final String TAG = "MainActivity";

    public static  final  String FILED_ID = "id";
    public static  final  String FILED_sex = "sex";
    public static  final  String FILED_age = "age";
    public static  final  String FILED_USERNAME = "USERNAME";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ContentResolver contentResolver =getContentResolver();
        Uri uri =Uri.parse("content://com.example.contentprvider3.Provider.UserProvider");
        contentResolver.registerContentObserver(uri, true, new ContentObserver(new Handler()) {
            @Override
            public void onChange(boolean selfChange) {
                super.onChange(selfChange);
                Log.d(TAG,"用户数据发生变化");

            }
        });


    }

    public void RemoteUser(View view) {
        Log.d(TAG, "onClick");

        try {

        ContentResolver contentResolver = getContentResolver();
         Cursor cursor = contentResolver.query(uri_provider, null, null, null, null);
            Log.d(TAG, "cursor------"+cursor);
            String[] columnNames2 = cursor.getColumnNames();
            for (String columnName : columnNames2) {
                Log.d(TAG, "columnName---" + columnName);
            }

            while (cursor.moveToNext()) {
                Log.d(TAG, "------------");
                for (String columnName : columnNames2) {
                    int columnname = cursor.getColumnIndex(columnName);
                    String value = cursor.getString(columnname);
                    Log.d(TAG, columnName + "------" + value);
                    Log.d(TAG, "----------------");
                }

            }
            cursor.close();
        }catch (Exception e){e.printStackTrace();
        }

    }
public void insert(View view){
        ContentResolver contentResolver =getContentResolver();
        Uri uri = Uri.parse("content://com.example.contentprvider3.Provider.UserProvider");
    ContentValues values = new ContentValues();
        values.put(FILED_age,19);
        values.put(FILED_sex,"male");
        values.put(FILED_USERNAME,"GE");
        values.put(FILED_ID,2003);

        contentResolver.insert(uri,values);
}

         }

5.TestProvider的manifest文件代码


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testprovider">
    
    <uses-permission android:name="string"/>
    <uses-permission android:name="string"/>
    
<queries>
    <package android:name="com.example.contentprvider3"/>
    <provider android:authorities="com.example.contentprvider3.UserProvider"/>

</queries>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.TestProvider">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

1.Log的报错部分

2021-11-29 11:06:00.493 4849-4849/com.example.testprovider E/ActivityThread: Failed to find provider info for com.example.contentprvider3.Provider.UserProvider
2021-11-29 11:06:00.493 4849-4849/com.example.testprovider D/MainActivity: cursor------null
2021-11-29 11:06:00.493 4849-4849/com.example.testprovider W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String[] android.database.Cursor.getColumnNames()' on a null object reference
2021-11-29 11:06:00.493 4849-4849/com.example.testprovider W/System.err:     at com.example.testprovider.MainActivity.RemoteUser(MainActivity.java:51)
2021-11-29 11:06:00.493 4849-4849/com.example.testprovider W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2021-11-29 11:06:00.493 4849-4849/com.example.testprovider W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
2021-11-29 11:06:00.493 4849-4849/com.example.testprovider W/System.err:     at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
2021-11-29 11:06:00.493 4849-4849/com.example.testprovider W/System.err:     at android.view.View.performClick(View.java:4780)
2021-11-29 11:06:00.493 4849-4849/com.example.testprovider W/System.err:     at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
2021-11-29 11:06:00.493 4849-4849/com.example.testprovider W/System.err:     at android.view.View$PerformClick.run(View.java:19866)
2021-11-29 11:06:00.493 4849-4849/com.example.testprovider W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
2021-11-29 11:06:00.493 4849-4849/com.example.testprovider W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
2021-11-29 11:06:00.493 4849-4849/com.example.testprovider W/System.err:     at android.os.Looper.loop(Looper.java:135)
2021-11-29 11:06:00.493 4849-4849/com.example.testprovider W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5254)
2021-11-29 11:06:00.493 4849-4849/com.example.testprovider W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2021-11-29 11:06:00.493 4849-4849/com.example.testprovider W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
2021-11-29 11:06:00.494 4849-4849/com.example.testprovider W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
2021-11-29 11:06:00.494 4849-4849/com.example.testprovider W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)



    public void RemoteUser(View view) {

        Log.d(TAG, "onClick");

 

        try {

 

        ContentResolver contentResolver = getContentResolver();

         Cursor cursor = contentResolver.query(uri_provider, null, null, null, null);

            Log.d(TAG, "cursor------"+cursor);

if(cursor == null){
return;
}

            String[] columnNames2 = cursor.getColumnNames();

            for (String columnName : columnNames2) {

                Log.d(TAG, "columnName---" + columnName);

            }

 

            while (cursor.moveToNext()) {

                Log.d(TAG, "------------");

                for (String columnName : columnNames2) {

                    int columnname = cursor.getColumnIndex(columnName);

                    String value = cursor.getString(columnname);

                    Log.d(TAG, columnName + "------" + value);

                    Log.d(TAG, "----------------");

                }

 

            }

            cursor.close();

        }catch (Exception e){e.printStackTrace();

        }

 

    }

private static final String AUTHORITY="com.example.contentprvider3.Provider.UserProvider";
改为 private static final String AUTHORITY="com.example.contentprvider3.UserProvider";