- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 android 中创建一个数据库,并从另一个文件中的 onCreateInputView() 调用 DBManager 中定义的创建函数。
以下是所需的文件。刚接触安卓,不太确定发生了什么。我必须做什么?这个错误是什么意思?
DBManager.java
package com.project.pesit.finaltelim;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import android.app.LauncherActivity;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBManager extends SQLiteOpenHelper {
private SQLiteDatabase db;
private final Context context;
private static final String DATABASE_NAME = "gredict";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE1 ="CREATE TABLE IF NOT EXISTS wordindex (wid INTEGER(4), word VARCHAR(256), gremeaning VARCHAR(1024), isFav TINYINT(1));";
private static final String DATABASE_CREATE2 ="CREATE TABLE IF NOT EXISTS lists (wid INTEGER(4), sid INTEGER(8), isDefault TINYINT(1));";
private static final String DATABASE_CREATE3 ="CREATE TABLE IF NOT EXISTS gloss (sid INTEGER(8), meaning VARCHAR(1024), pos INTEGER(1));";
public DBManager(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context=context;
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE1);
db.execSQL(DATABASE_CREATE2);
db.execSQL(DATABASE_CREATE3);
BufferedReader br = null;
String sCurrentLine;
//Populate word index table
try
{
Resources res=context.getResources();
InputStream is = res.openRawResource(R.raw.wordindex);
br = new BufferedReader(new InputStreamReader(is));
while ((sCurrentLine = br.readLine()) != null) {
db.execSQL(sCurrentLine);
}
}
catch
(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (br != null)br.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
//Populate lists table
try
{
Resources res=context.getResources();
InputStream is = res.openRawResource(R.raw.list);
br = new BufferedReader(new InputStreamReader(is));
while ((sCurrentLine = br.readLine()) != null) {
db.execSQL(sCurrentLine);
}
}
catch
(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (br != null)br.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
//Populate gloss table
try
{
Resources res=context.getResources();
InputStream is = res.openRawResource(R.raw.gloss);
br = new BufferedReader(new InputStreamReader(is));
while ((sCurrentLine = br.readLine()) != null) {
db.execSQL(sCurrentLine);
}
}
catch
(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (br != null)br.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
onCreate(db);
}
public Boolean open() throws SQLException
{
db = this.getWritableDatabase();
return true;
}
public void close()
{
db.close();
}
public List<String> read(String box)
{
List<String> wordList = new ArrayList<String>();
String query;
Cursor cursor=null;
if(box.equals("HF"))
{
query = "SELECT distinct word FROM wordindex WHERE wid<335";
}
else
{
query = "SELECT distinct word FROM wordindex WHERE word LIKE '"+box+"%' and wid>334";
}
cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
String word;
word=cursor.getString(cursor.getColumnIndexOrThrow("word"));
wordList.add(word);
} while (cursor.moveToNext());
}
cursor.close();
return wordList;
}
public List<String[]> getWord(String name){
// TODO Auto-generated method stub
//String[] cols=new String []{WORD,MEAN};
//String sid=String.valueOf(id);
List<String[]> meanings = new ArrayList<String[]>();
Cursor c1=db.rawQuery("SELECT DISTINCT (sid) FROM lists WHERE wid = (SELECT wid FROM wordindex where word='"+name+"')",null);
if (c1.moveToFirst()) {
do {
int sid=c1.getColumnIndex("sid");
Cursor c2=db.rawQuery("SELECT pos, meaning FROM gloss WHERE sid = "+c1.getString(sid),null);
if (c2.moveToFirst()) {
do {
int ipos=c2.getColumnIndex("pos");
int imean=c2.getColumnIndex("meaning");
String pos=null;
if(c2.getInt(ipos)==0)
pos="(n)";
else if(c2.getInt(ipos)==1)
pos="(v)";
else if(c2.getInt(ipos)==2)
pos="(adj)";
String mean[] = {pos,c2.getString(imean)};
meanings.add(mean);
} while (c2.moveToNext());
}
} while (c1.moveToNext());
}
return meanings;
}
public String getGREMeaning(String name){
String meaning=null;
Cursor c=db.rawQuery("SELECT gremeaning FROM wordindex where word='"+name+"'",null);
c.moveToFirst();
int imean=c.getColumnIndex("gremeaning");
meaning=c.getString(imean);
return meaning;
}
public String getGREMeaningWID(int wid){
String meaning=null;
Cursor c=db.rawQuery("SELECT gremeaning FROM wordindex where wid='"+wid+"'",null);
c.moveToFirst();
int imean=c.getColumnIndex("gremeaning");
meaning=c.getString(imean);
return meaning;
}
public List<ListItem> getList(String word) {
List<ListItem> l=new ArrayList<ListItem>();
int groupcount;
Cursor c=db.rawQuery("SELECT * FROM lists where wid in(SELECT wid from wordindex where word='"+word+"')",null);
int imean=c.getColumnIndex("sid");
for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
{ Cursor s=db.rawQuery("SELECT distinct wid FROM lists where sid="+c.getString(imean),null);
int index=s.getColumnIndexOrThrow("wid");
groupcount=s.getCount();
if(groupcount>1){
ListItem li=new ListItem();
li.setSID(c.getString(imean));
for(s.moveToFirst();!s.isAfterLast();s.moveToNext())
{
li.addWID(s.getString(index));
}
l.add(li);
}
}
return l;
}
public List<ListItem> getGroupList() {
List<ListItem> l=new ArrayList<ListItem>();
Cursor c=db.rawQuery("SELECT sid,meaning FROM v2",null);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
{
ListItem li=new ListItem();
li.setSID(c.getString(c.getColumnIndex("sid")));
li.setGloss(c.getString(c.getColumnIndex("meaning")));
l.add(li);
}
/*
Cursor c=db.rawQuery("SELECT sid FROM `v` WHERE `count` >1",null);
int imean=c.getColumnIndex("sid");
for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
{
Cursor s=db.rawQuery("SELECT sid,meaning FROM gloss where sid="+c.getString(imean),null);
s.moveToFirst();
ListItem li=new ListItem();
li.setSID(s.getString(s.getColumnIndex("sid")));
li.setGloss(s.getString(s.getColumnIndex("meaning")));
l.add(li);
}
*/
return l;
}
public String getGloss(String sid)
{
Cursor c=db.rawQuery("select meaning from gloss where sid="+sid,null);
int i=c.getColumnIndex("meaning");
if(c.moveToFirst())
{
return c.getString(i);
}
return "null";
}
public String getwd(String wid)
{
Cursor c=db.rawQuery("select word from wordindex where wid="+wid,null);
int i=c.getColumnIndex("word");
if(c.moveToFirst())
{
return c.getString(i);
}
return "null";
}
public List<String> getGroupWords(String sid)
{
List<String> wordList = new ArrayList<String>();
String query;
Cursor cursor=null;
query = "SELECT wid,isDefault FROM lists WHERE sid="+sid;
cursor = db.rawQuery(query, null);
int iwid=cursor.getColumnIndexOrThrow("wid");
int defid=cursor.getColumnIndexOrThrow("isDefault");
if (cursor.moveToFirst()) {
do {
Cursor c=db.rawQuery("SELECT word FROM wordindex WHERE wid="+cursor.getString(iwid),null);
c.moveToFirst();
String word;
word=c.getString(c.getColumnIndexOrThrow("word"));
wordList.add(word+":"+cursor.getString(defid));
} while (cursor.moveToNext());
}
cursor.close();
return wordList;
}
public String addToGroup(String sid, String word)
{
String wid=null;
Cursor cursor=null;
String query = "SELECT wid FROM wordindex WHERE word='"+word+"'";
cursor = db.rawQuery(query, null);
String result=null;
if (cursor.moveToFirst()) {
wid=cursor.getString(cursor.getColumnIndexOrThrow("wid"));
cursor.close();
query = "SELECT * FROM lists WHERE wid="+wid+" AND sid="+sid;
cursor = db.rawQuery(query, null);
if(cursor.moveToFirst())
{
result="Word already in the group.";
}
else
{
query = "INSERT INTO lists VALUES("+wid+", "+sid+",0);";
try{
db.execSQL(query);
result="Added successfully";
}
catch(Exception e)
{
result="Could not add the word.";
}
}
}
return result;
}
public String newGroup(String meaning, String pos, String word)
{
if(pos.equals("Noun"))
pos="0";
else if(pos.equals("Verb"))
pos="1";
else if(pos.equals("Adjective"))
pos="2";
String wid;
int max;
String query = "SELECT max(sid) FROM lists;";
Cursor cursor = db.rawQuery(query, null);
String result=null;
cursor.moveToFirst();
max=Integer.parseInt(cursor.getString(cursor.getColumnIndexOrThrow("max(sid)")));
cursor.close();
query = "SELECT wid FROM wordindex WHERE word='"+word+"'";
cursor = db.rawQuery(query, null);
cursor.moveToFirst();
wid=cursor.getString(cursor.getColumnIndexOrThrow("wid"));
cursor.close();
try{
query = "INSERT INTO lists VALUES ("+wid+", "+(max+1)+",0);";
db.execSQL(query);
query = "INSERT INTO gloss VALUES ("+(max+1)+", '"+meaning+"',"+pos+");";
db.execSQL(query);
db.execSQL("DROP VIEW v1;");
db.execSQL("CREATE VIEW v1 AS SELECT sid, isDefault, COUNT(sid) as count FROM lists GROUP BY sid;");
db.execSQL("DROP VIEW v2;");
db.execSQL("CREATE VIEW v2 AS SELECT sid,meaning FROM gloss where sid IN (SELECT sid FROM v1 WHERE count>1);");
result="Created successfully";
}
catch(Exception e)
{
result="Could not create group.";
}
return result;
}
public List<String> search(String text,String box)
{
List<String> wordList = new ArrayList<String>();
String query;
Cursor cursor=null;
if(box.equals("HF"))
{
query = "SELECT distinct word FROM wordindex WHERE wid<335 and word LIKE '"+text+"%'";
}
else
{
query = "SELECT distinct word FROM wordindex WHERE word LIKE '"+box+"%' and wid>334 and word LIKE'"+text+"%'";
}
cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
String word;
word=cursor.getString(cursor.getColumnIndexOrThrow("word"));
// Adding word to list
// Toast.makeText(getApplicationContext(), word, Toast.LENGTH_LONG).show();
wordList.add(word);
} while (cursor.moveToNext());
}
cursor.close();
return wordList;
}
public List<ListItem> searchGroup(String text)
{
List<ListItem> l=new ArrayList<ListItem>();
//List<String> wordList = new ArrayList<String>();
String query;
Cursor c=null;
query = "SELECT * FROM wordindex WHERE word LIKE'"+text+"%'";
c = db.rawQuery(query, null);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
{
Cursor s=db.rawQuery("SELECT sid,meaning FROM gloss where sid in(SELECT sid from lists where wid="+c.getString(c.getColumnIndexOrThrow("wid"))+")",null);
for(s.moveToFirst();!s.isAfterLast();s.moveToNext())
{
//s.moveToFirst();
ListItem li=new ListItem();
li.setSID(s.getString(s.getColumnIndex("sid")));
li.setGloss(s.getString(s.getColumnIndex("meaning")));
l.add(li);
}
}
c.close();
return l;
}
public String getID(String word1)
{
Cursor c=db.rawQuery("select wid from wordindex where word='"+word1+"'",null);
int i=c.getColumnIndex("wid");
if(c.moveToFirst())
{
return c.getString(i);
}
return "null";
}
public List<String> getFavList()
{
List<String> wordList = new ArrayList<String>();
String query;
Cursor cursor=null;
query = "SELECT distinct word FROM wordindex WHERE isFav=1";
cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
String word;
word=cursor.getString(cursor.getColumnIndexOrThrow("word"));
wordList.add(word);
} while (cursor.moveToNext());
}
cursor.close();
return wordList;
}
public Boolean isFav(String word)
{
Cursor c=db.rawQuery("SELECT isFav FROM wordindex WHERe word='"+word+"'",null);
int i=c.getColumnIndex("isFav");
if(c.moveToFirst())
{
if(c.getString(i).equals("1"))
{
return true;
}
else
{
return false;
}
}
return false;
}
public void toggleFav(String word, Boolean isFav)
{
String fav;
if(isFav)
{
fav="1";
}
else
{
fav="0";
}
db.execSQL("UPDATE wordindex SET isFav="+fav+" WHERE word='"+word+"'");
}
public int getWordCount()
{
Cursor c=db.rawQuery("SELECT MAX(wid) FROM wordindex",null);
int i=c.getColumnIndex("MAX(wid)");
if(c.moveToFirst())
{
return c.getInt(i);
}
else
{
return 0;
}
}
public String getBoxCount(String box)
{
//List<String> wordList = new ArrayList<String>();
String query;
String query1;
String result="";
Cursor cursor=null;
Cursor cursor1=null;
if(box.equals("HF"))
{
return "1@334";
}
else
{ query1 = "SELECT MIN(wid) AS count FROM wordindex WHERE word LIKE '"+box+"%' and wid>334";
query = "SELECT MAX(wid) AS count FROM wordindex WHERE word LIKE '"+box+"%' and wid>334";
}
cursor = db.rawQuery(query, null);
cursor1 = db.rawQuery(query1, null);
if (cursor1.moveToFirst()) {
result+=cursor1.getString(cursor1.getColumnIndexOrThrow("count"));
}
if (cursor.moveToFirst()) {
result+="@"+cursor.getString(cursor.getColumnIndexOrThrow("count"));
}
cursor.close();
return result;
}
public String removefromgroup(String item,String sid)
{
String result;
String query;
Log.d("ITEM",item);
Log.d("MEANING",getGloss(sid));
String wid=getID(item);
query="DELETE FROM lists WHERE wid="+wid+" AND sid="+sid;
try{
db.execSQL(query);
result=item+" removed successfully";
}
catch(Exception e)
{
e.printStackTrace();
result="Could not delete the word.";
}
return result;
}}
onCreateInputView.java
@Override public View onCreateInputView() {
mInputView = (KeyboardView) getLayoutInflater().inflate(
R.layout.input, null);
mInputView.setOnKeyboardActionListener(this);
mInputView.setKeyboard(mQwertyKeyboard);
DBManager mDBAdapter = new DBManager(getApplicationContext());
// boolean a=mDBAdapter.checkDatabase(getApplicationContext());
//if (!mDBAdapter.checkDatabase(getApplicationContext())) {
// try{
// sampleDB=getApplicationContext().openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
//
// }catch(SQLiteException e){
// Toast.makeText(getApplicationContext(), "Could not create database", Toast.LENGTH_SHORT).show();
// }sampleDB.close();
mDBAdapter.onCreate(sampleDB);
Toast.makeText(getApplicationContext(), "Database Created", Toast.LENGTH_SHORT).show();
// }else{
//Toast.makeText(getApplicationContext(), "Available", Toast.LENGTH_SHORT).show();
//}
return mInputView;
}
错误日志如下:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)' on a null object reference
at com.project.pesit.finaltelim.DBManager.onCreate(DBManager.java:44)
at com.project.pesit.finaltelim.SoftKeyboard.onCreateInputView(SoftKeyboard.java:118)
at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1108)
at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:1493)
at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:1468)
at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:433)
at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:203)
at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
最佳答案
您永远不应该自己调用 onCreate
方法,SQLiteOpenHelper
类会自动为您执行此操作。
从 onCreateInputView.java 的代码中删除此行:
mDBAdapter.onCreate(sampleDB);
关于java - 空指针异常: Attempt to invoke virtual method on a null object reference Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29809130/
进程虚拟机和系统虚拟机有什么区别? 我的猜测是,进程 VM 没有为该操作系统的整个应用程序提供一种操作系统,而是为某些特定应用程序提供环境。 系统虚拟机为操作系统提供了一个安装环境,就像 Virtua
我在成员函数的上下文中理解 virtual,例如 virtual void frob()。但它在类声明的上下文中意味着什么,例如 class Foo : public virtual Bar? 对于给
根据 react-virtualized 文档,“AutoSizer 组件装饰 React 元素并自动管理宽度和高度属性,以便装饰元素填充可用空间”。 建议通常是加上height: 100%;或 fl
根据 this类似 StackOverflow 问题和其他文章,默认情况下 C# 方法是“非虚拟的”,我认为这意味着您不能在派生类中覆盖它们。 如果那是真的,能否请您向我解释一下,在下面的示例中,我如
我有一个基类Media和几个派生类,即DVD、Book等...基类写成: class Media{ private: int id; string title;
我搜索了一些关于虚函数声明的帖子,相信 =0 在 virtual void test()=0; 是固定句法所以 virtual void test()=NULL; virtual void test(
我正在使用 RV 列表加载具有自定义格式的大型文档。它非常有效,但我遇到了以下两个问题: 我目前在 cellmeasurer 中设置了一个列表 based on this计算行的动态高度(宽度是固定的
我一直在努力制作 this react virtualized table example工作 & 开始严重怀疑我的理智。我创建了一个 react 应用程序,我只是想在 App.js 中使用以下内容呈
我在Windows 7 Pro计算机上安装了Windows Virtual PC和Windows XP Mode。运行XP模式会在Virtual PC上自动安装XP。我想创建第二台与第一台相同的虚拟P
我使用 Virtual PC 来创建新的环境来测试我的安装程序。但我一定是做错了什么,因为内部装有 Vista 或 XP 的 VPC 镜像占用了大约 15GB 的磁盘空间(包括安装在其中的 VS200
是否可以为 Ubuntu 虚拟机动态分配处理器和内存?例如。进程在主机系统上运行,导致处理器的使用率从 30%-70% 上下波动,这些进程还占用 8GB 内存中 3GB-7GB 之间的波动量,即 1G
我正在使用“react-virtualized”来创建一个表。在该表中,一些数据可能显示为 'Brian Vaughn1'。 .此表格单元格应具有 font-weight: bold并且只应呈现文本,
我正在使用“react-virtualized”来创建一个表。在该表中,一些数据可能显示为 'Brian Vaughn1'。 .此表格单元格应具有 font-weight: bold并且只应呈现文本,
我一直在努力理解一段这样的代码: class A { // some class definition } class B { public: virtual A *s
基于 http://en.wikipedia.org/wiki/Virtual_inheritance class Animal { ... }; // Two classes virtually i
我看到 C++ 中的某些函数被声明为 virtual const int getNumber(); 但是如果函数声明如下有什么区别呢? const virtual int getNumber(); 这
问题来自C++ faq。 http://www.parashift.com/c++-faq-lite/protected-virtuals.html 使用公共(public)重载虚拟的代码: clas
这个问题在这里已经有了答案: How is "=default" different from "{}" for default constructor and destructor? (3 个答案
virtual final 函数(final 在基类)是否有任何 vtable/virtual 成本? class B{ public: virtual void fFinal() final
我有一个只包含 exe 文件(没有源代码)的 hello 工具。 你好工具结构: bin helloBin.exe helloRoot.exe conanfile.py conanfile.py
我是一名优秀的程序员,十分优秀!