简体   繁体   English

MyContentProvider.onCreate()没有被调用?

[英]MyContentProvider.onCreate() is not being called?

The onCreate of my userDatabase that extends ContentProvider is not properly called 扩展ContentProvider的userDatabase的onCreate没有正确调用

Here is some of my userBatabase code: 这是我的一些userBatabase代码:

public class userDatabase extends ContentProvider {

    private MainDatabaseHelper mOpenHelper;

    public userDatabase(){}

    public static final class MainDatabaseHelper extends SQLiteOpenHelper{...}

    @Override
    public boolean onCreate() {
        mOpenHelper = new MainDatabaseHelper(getContext());     
        return true;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        long id = mOpenHelper.getWritableDatabase().insert("Users", null, values);
        return Uri.withAppendedPath(CONTENT_URI, "" + id);
    }

    ...
}

In my main activity I call: 在我的主要活动中,我称:

userDatabase cpDatabase = new userDatabase();

But when I try to call cpDatabase.insert(userDatabase.CONTENT_URI, values); 但是当我尝试调用cpDatabase.insert(userDatabase.CONTENT_URI, values);

Everything crashes inside insert when mOpenHelper.getWritableDatabase().insert("Users", null, values); mOpenHelper.getWritableDatabase().insert("Users", null, values);插入时,所有内容都会崩溃mOpenHelper.getWritableDatabase().insert("Users", null, values); is called. 叫做。

I found out that mOpenHelper.getWritableDatabase() was the issue, as it wont run even by itself, and then I found out this was because mOpenHelper was null. 我发现mOpenHelper.getWritableDatabase()是问题所在,因为它甚至无法mOpenHelper.getWritableDatabase()运行,然后我发现这是因为mOpenHelper为空。

I instantiate mOpenHelper in the constructor, so I figure its not running. 我在构造函数中实例化了mOpenHelper,所以我认为它没有运行。 A few log messages confirm this, when I call userDatabase cpDatabase = new userDatabase(); 当我调用userDatabase cpDatabase = new userDatabase();时,一些日志消息确认了这一点userDatabase cpDatabase = new userDatabase(); my log messages showed that the userDatabase() constructor ran normally, but the onCreate never ran, so the mOpenHelper never got instantiated. 我的日志消息显示userDatabase()构造函数正常运行,但是onCreate从未运行,因此mOpenHelper从未实例化。

(Note: with these log messages, I noticed that the constructor and the onCreate for my userDatabase got called when my app started. I have no idea why or where. I dont understand why this was run before i tried to create an instance. and even though it was run, mOpenHelper still wasn't initialized, and when i created an instance, the constructor ran but the onCreate didnt.) (注意:通过这些日志消息,我注意到我的应用程序启动时调用了我的userDatabase的构造函数和onCreate。我不知道为什么或在哪里。我不明白为什么在尝试创建实例之前运行了它。即使它已运行,但mOpenHelper仍未初始化,并且当我创建实例时,构造函数已运行,但onCreate却没有。)

What could possibly be happening, and how can I make my onCreate run? 可能会发生什么,如何使onCreate运行?

Since you are using content providers, according to the documentation 由于您使用的是内容提供程序,因此根据文档

This method is called for all registered content providers on the application main thread at application launch time 在应用程序启动时,将为应用程序主线程上的所有注册内容提供者调用此方法。

And the way you try to use the content provider is seems wrong and, You don't need to manually instantiate the content provider, once you made the request via the ContentResolver by passing the URI, the system inspects the authority of the given URI and passes the request to the content provider registered with the authority. 而且您尝试使用内容提供者的方式似乎是错误的,并且,您无需手动实例化内容提供者,一旦通过ContentResolver通过传递URI发出请求,系统就会检查给定URI的权限,并将请求传递给在授权机构注册的内容提供商。

for example 例如

getContentResolver().delete(uri, null, null);

Where the uri is, the full URI to query. uri在哪里,是要查询的完整URI。

This tutorial will guide you in right direction 教程将指导您正确的方向

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

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