简体   繁体   English

传递上下文并在服务中使用SQL

[英]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关键字实例化一个ActivityService 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.

相关问题 需要在Jersey REST服务中使用SQLiteOpenHelper,但是我无法传递所需的Android上下文 - Need to use SQLiteOpenHelper in a Jersey REST service but I cannot pass required Android context 使用这种方式将上下文传递给非上下文类是否安全 - Is it safe to use this way to pass context to non-context classes 如何使用服务的上下文创建ImageView - How to use the context of a Service to create ImageView Android将String形式的Service从Service传递到Service?并在Service中的onCreate上使用? - Android Pass String form Service to Service?And use at onCreate in Service? 如何在Talend Open Studio作业中将SQL查询作为上下文变量传递 - How to pass SQL query as a context variable in Talend Open Studio job 将上下文传递到非活动类以使用AssetManager.getAssets() - Pass context to a non-Activity Class to use AssetManager.getAssets() 传递 Context 或使用 view.getContext()。 真的有关系吗? - Pass Context or make use of view.getContext(). Does it really matter? 在服务接收器中用于实例化对象的适当上下文是什么? - What's the proper context to use in service receiver to instantiate objects? 如何在CXF Web服务中将根上下文用作wsdl端点 - How to use root context as wsdl end point in CXF Web service 将上下文传递给异步记录器 - Pass context to async Logger
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM