简体   繁体   中英

onCreate() in ContentProvider is not being called

onCreate() method in ContentProvider is not being called. Though when I click on a button, it goes inside insert() method and returns java.lang.NullPointerException . In onCreate() method I have only have

@Override
public boolean onCreate() {
        Log.v(TAG,"Inside create provider");
        database = new DatabaseSQLiteMyDB(getContext());
        return false;
}

Log CAT shows:

04-20 01:23:00.543: V/CLICKED(1607): CLICKED
04-20 01:23:00.553: V/Provider(1607): inside insert
04-20 01:23:00.573: V/database(1607): created
04-20 01:23:00.573: E/pkg.OnPut_ClickListener(1607): java.lang.NullPointerException
04-20 01:23:00.583: V/pkg.OnPut_ClickListener(1607): In exception code

in Manifest :

<provider android:name="somepkg.CustomContentProvider"
            android:authorities="somepkg.provider" />

While creating URI upon click :

{
    myURI=buildUri("content", "somepkg.provider");
            this.putNo=putNo;
            mContentValues = initTestValues();
    }
        private Uri buildUri(String scheme, String authority) {
            Uri.Builder uriBuilder = new Uri.Builder();
            uriBuilder.authority(authority);
            uriBuilder.scheme(scheme);
            return uriBuilder.build();
        }

You need to show the code where you perform the insert.

Sorry the rest of this is not targeted directly at the question, but it is something I've seen all over the place in code samples about ContentProviders.

You should not call database = new DatabaseSQLiteMyDB(getContext()); in the ContentProvider onCreate. This is for the reason given in SQLiteOpenHelper documentation for getReadableDatabase and getWritableDatabase:

Like getWritableDatabase, this method may take a long time to return, so you should not call it from the application main thread, including from ContentProvider.onCreate().

Instead hold a reference to a SQLiteOpenHelper instance and call getWritableDatabase/getReadableDatabase as appropriate in the query/insert/delete/update methods which are called asynchronously if you are using Loaders.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM