【发布时间】:2017-08-26 22:25:46
【问题描述】:
这里有一点背景。在我的应用程序中查询数据库 (QueryDB) 的过程从 MainActivity.onCreate 开始,我有这段代码:
assets = getAssets(); // the SQLite database
DatabaseConnector
dbc = new DatabaseConnector(getApplicationContext(), assets);
dbc.setDbProcesslistener(this); // set way to know matches has been defined
dbc.findDBMatches();
在我的问题(名为DatabaseConnector)的文件中,我有:
void findDBMatches()
{
mContext.startService(new Intent(mContext, QueryDB.class));
}
这就是问题所在。这个代码段...
public static class QueryDB extends IntentService
{
public QueryDB(String name)
{
super(name);
}
导致此错误:
? E/libprocessgroup: failed to make and chown /acct/uid_10058: Read-only file system
? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
com.dslomer64.servyhelperton E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dslomer64.servyhelperton, PID: 335
java.lang.RuntimeException: Unable to instantiate service
com.dslomer64.servyhelperton.DatabaseConnector$QueryDB:
java.lang.InstantiationException: class
com.dslomer64.servyhelperton.DatabaseConnector$QueryDB 没有零参数构造函数
错误没有告诉我一行,但确实提到了QueryDB。所以我为QueryDB 插入一个零参数构造函数并立即得到一个错误:
我圈出了extends IntentService,因为在findDBMatches 中,我为QueryDB 启动了一项服务,并且第一条错误消息显示Unable to instantiate service。但是,在调试中,我发现在mContext.startService(new Intent(mContext, QueryDB.class)); 行处执行并没有失败。我在 QueryDB 的构造函数中设置了断点,但没有执行。
我迷路了。
在(愚蠢地)接受 AS 的更改建议之前,该应用运行良好。现在我可以回顾历史并恢复到开始更改之前的版本,但是有很多(警告)我已经摆脱了,我宁愿不这样做。 如果有人可以用这么短的 sn-ps 代码提出修复建议,我可以尝试一下,也许会很好。
补充说明:
如果没有 QueryDB 扩展 IntentService,应用将无法运行(出现其他即时错误)。
请注意,mContext 被声明为 static(由于内存泄漏,我现在知道不要这样做),但它是否为 static 并不重要。同样的错误。
【问题讨论】:
标签: android android-intent service database-connection intentservice