[英]Pass Context and use SQL in service
I want to write some SQL to a database, however when executing it I get a nullpointer error. 我想向数据库中写入一些SQL,但是执行它时会出现nullpointer错误。 I think I do something wrong with passing the Context from
WhatsApi.java
to MessageService.java
. 我想将Context从
WhatsApi.java
传递到MessageService.java
WhatsApi.java
了。 (See the Context.MODE_PRIVATE
) (请参见
Context.MODE_PRIVATE
)
This part from MessageService.java
这部分来自
MessageService.java
db = openOrCreateDatabase("msgstore", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS messages(`from` TEXT, `to` TEXT, message TEXT, id TEXT, t TEXT);");
This in WhatsApi.java
在
WhatsApi.java
MessageService msg = new MessageService();
msg.saveMessage(mContext, "mynumber", to, message, id, time());
I suggest you guys to see this link too: 我建议你们也看到此链接:
https://github.com/gi097/WhatsApi-Android/commit/3875d97458095f792c73f275648629aaaf726751 https://github.com/gi097/WhatsApi-Android/commit/3875d97458095f792c73f275648629aaaf726751
Any help is appreciated. 任何帮助表示赞赏。 Sorry for the maybe unclear look, but I hope you guys understand.
抱歉,您可能不清楚自己的样子,但我希望你们能理解。
Well I use this in one of my Projects DatabaseHandler dbHandler=null;//Database handler class//global var SQLiteDatabase contactDatabase=null;// globalvar . . //some function{ if(dbHandler==null) dbHandler = new DatabaseHandler(this);//service context if(contactDatabase==null) contactDatabase=dbHandler.getWritableDatabase(); ArrayList<information>list= new ArrayList<information>(); String selectQuery = "SELECT * FROM " + DatabaseHandler.TABLE_CONTACTS; Cursor cursor = contactDatabase.rawQuery(selectQuery, null);
好吧,我在我的Projects
DatabaseHandler dbHandler=null;//Database handler class//global var SQLiteDatabase contactDatabase=null;// globalvar . . //some function{ if(dbHandler==null) dbHandler = new DatabaseHandler(this);//service context if(contactDatabase==null) contactDatabase=dbHandler.getWritableDatabase(); ArrayList<information>list= new ArrayList<information>(); String selectQuery = "SELECT * FROM " + DatabaseHandler.TABLE_CONTACTS; Cursor cursor = contactDatabase.rawQuery(selectQuery, null);
DatabaseHandler dbHandler=null;//Database handler class//global var SQLiteDatabase contactDatabase=null;// globalvar . . //some function{ if(dbHandler==null) dbHandler = new DatabaseHandler(this);//service context if(contactDatabase==null) contactDatabase=dbHandler.getWritableDatabase(); ArrayList<information>list= new ArrayList<information>(); String selectQuery = "SELECT * FROM " + DatabaseHandler.TABLE_CONTACTS; Cursor cursor = contactDatabase.rawQuery(selectQuery, null);
You should never instantiate an Activity
or Service
yourself with the new
keyword. 您绝不可以使用
new
关键字实例化一个Activity
或Service
。 It is Android's responsibility to start these components because Android needs to also do some additional setup, including provide a base Context
for these components. 启动这些组件是Android的责任,因为Android还需要进行一些其他设置,包括为这些组件提供基础
Context
。 That is why you get a NullPointerException
in your service class. 这就是为什么您在服务类中得到
NullPointerException
原因。 You have to actually use startService()
to make the service run properly. 您必须实际使用
startService()
才能使服务正常运行。
In my opinion, since WhatsApi
already has a Context, I don't see any reason you need to bother having the service save the data. 我认为,由于
WhatsApi
已经具有上下文,因此我看不到您需要麻烦服务保存数据的任何原因。 Just open the database and save the data in WhatsApi
. 只需打开数据库并将数据保存在
WhatsApi
。
By the way, since you're using SQLite, you really should be using SQLiteOpenHelper
instead of calling methods like openOrCreateDatabase()
directly. 顺便说一句,由于您正在使用SQLite,因此您确实应该使用
SQLiteOpenHelper
而不是直接调用诸如openOrCreateDatabase()
类的方法。 See this guide for more information. 有关更多信息,请参见本指南 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.