简体   繁体   English

ContentProvider中的onCreate()未被调用

[英]onCreate() in ContentProvider is not being called

onCreate() method in ContentProvider is not being called. ContentProvider onCreate()方法没有被调用。 Though when I click on a button, it goes inside insert() method and returns java.lang.NullPointerException . 虽然当我单击按钮时,它进入insert()方法内部并返回java.lang.NullPointerException In onCreate() method I have only have onCreate()方法中,我只有

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

Log CAT shows: Log CAT显示:

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 : 在点击创建URI时:

{
    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. 抱歉,其余内容并非直接针对该问题,但这是我在有关ContentProviders的代码示例中到处都看到的。

You should not call database = new DatabaseSQLiteMyDB(getContext()); 您不应调用database = new DatabaseSQLiteMyDB(getContext()); in the ContentProvider onCreate. 在ContentProvider onCreate中。 This is for the reason given in SQLiteOpenHelper documentation for getReadableDatabase and getWritableDatabase: 这是由于SQLiteOpenHelper文档中针对getReadableDatabase和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(). 与getWritableDatabase一样,此方法可能需要很长时间才能返回,因此您不应从应用程序主线程(包括从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. 而是保留对SQLiteOpenHelper实例的引用,并在查询/插入/删除/更新方法中适当地调用getWritableDatabase / getReadableDatabase,如果使用Loader,则该方法将被异步调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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