2

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();
        }
sattu
  • 632
  • 1
  • 22
  • 37

1 Answers1

0

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.

Protonflux
  • 139
  • 1
  • 5
  • Your post does not make sense to me. You say not to instantiate the `SQLiteOpenHelper` in `onCreate` but then cite the docs for getWritableDatabase/getReadableDatabase as your evidence. The documentation describes that the potentially long-running database creation is deferred until first use, and in fact says "if you are using an SQLite database you can create a new SQLiteOpenHelper object in ContentProvider.onCreate()" https://developer.android.com/guide/topics/providers/content-provider-creating.html#OnCreate – JHS Dec 10 '17 at 00:10