我正在构建一个搜索界面。数据库使用全文搜索来查询数据库。
我有一个电子书表 - 列是
title | author | publisher | edition content
“内容”列包含有关该书的说明。 在用户界面中,我有一个输入edittext框。在此框中,我输入一个单词或短语,单词或短语与列内容匹配(使用MATCH查询)。如果找到匹配项,则显示详细信息(即标题,作者,出版商,版本)。 我有一个主类和一个数据库类。但每次我都被例外:
java.lang.illegalstateexception: could not execute method of the activity
我提供的两个代码。
MAIN CLASS
package com.search.wordfinder;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClickChange(View v){
EditText searchText = (EditText)findViewById(R.id.edit_message);
String searchId = searchText.getText().toString();
setContentView(R.layout.nextpage);
ListView list=(ListView)findViewById(R.id.list);
DatabaseCreation dc = new DatabaseCreation(this);
ArrayList<String> rslt = dc.queryDatabase(searchId);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, rslt);
list.setAdapter(adapter);
dc.close();
}
public void onClickBack(View v){
setContentView(R.layout.main);
}
}
数据库类
package com.search.wordfinder;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.ContentValues;
import android.util.*;
public class DatabaseCreation extends SQLiteOpenHelper {
String sString;
SQLiteDatabase mDatabase;
String query;
public static final String DATABASE_NAME = "wor.db";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "cpy";
private static String DATABASE_CREATE = "CREATE VIRTUAL TABLE " + TABLE_NAME + " USING FTS3" + "(" + "title TEXT," + "author TEXT," + "publisher TEXT," + "edition TEXT," + "content TEXT" + ")";
public DatabaseCreation(Context context) {
super(context, DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try{
//db.delete(TABLE_NAME, null, null);
db.execSQL(DATABASE_CREATE);
insertData("Operating System", "PeterGalvin", "Willey", "Eight", "An operating System (OS) is a collection of software that manages computer hardware resources and provides common services for computer programs.");
insertData("Database Management System", "Korth", "McGraw-Hill", "Fourth", "A DataBase Management System (DBMS) is a software system that manages execution of users applications to access and modify database.");
} catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(DatabaseCreation.class.getName(),"Upgrading database from version" +
oldVersion + "to" + newVersion + ",which will destroy all old data");
db.execSQL("Drop table if exists" + TABLE_NAME);
onCreate(db);
}
public void insertData(String ttl, String athr, String pub, String ed, String cnt){
mDatabase = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("title", ttl);
values.put("author", athr);
values.put("publisher", pub);
values.put("edition", ed);
values.put("content", cnt);
mDatabase.insert(TABLE_NAME, null, values);
}
public ArrayList<String> queryDatabase(String id){
sString=id;
ArrayList<String> data = new ArrayList<String>();
try{
query = "SELECT title,author,publisher,edition FROM " + TABLE_NAME + " WHERE content MATCH '"+sString+"'";
Cursor c = mDatabase.rawQuery(query, null);
c.moveToFirst();
while(c.isAfterLast()==false){
String str = "Title: " + c.getString(0) +"\n"+ "Author: " + c.getString(1)+ "\n" + "Publisher: " + c.getString(2) + "\n" +
"Edition: " + c.getString(3);
data.add(str);
c.moveToNext();
}
c.close();
} catch (Exception e){
e.printStackTrace();
}
return data;
}
}
答案 0 :(得分:1)
堆栈跟踪说:
java.lang.NullPointerException
at com.search.wordfinder.DatabaseCreation.insertData(DatabaseCreation.java:59)
鉴于此方法的内容,第59行必须是以下内容:
mDatabase.insert(TABLE_NAME, null, values);
如果此行发生NullPointerException,则表示mDatabase
为空。因此,请检查此变量是否已在任何位置初始化。它不是:我没有看到像
mDatabase = ...;
初始化此变量。