Android 无法启动活动组件信息
我正在制作一个可以搜索“employee”表并返回结果的应用程序。我正在使用searchdialog进行搜索。但按下搜索对话框上的搜索按钮时,我会得到FC。请帮帮我 代码:Android 无法启动活动组件信息,android,sqlite,cursor,Android,Sqlite,Cursor,我正在制作一个可以搜索“employee”表并返回结果的应用程序。我正在使用searchdialog进行搜索。但按下搜索对话框上的搜索按钮时,我会得到FC。请帮帮我 代码: public class SimpleSearch extends ListActivity { protected SQLiteDatabase db; protected Cursor cursor; protected ListAdapter adapter; protected Str
public class SimpleSearch extends ListActivity {
protected SQLiteDatabase db;
protected Cursor cursor;
protected ListAdapter adapter;
protected String query;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}
private void doMySearch(String query) {
cursor = db.rawQuery("SELECT _id, firstName, lastName, title FROM employee WHERE firstName || ' ' || lastName LIKE ?",
new String[]{"%" + query + "%"});
adapter = new SimpleCursorAdapter(
this,
R.layout.emp_list_item,
cursor,
new String[] {"firstName", "lastName", "title"},
new int[] {R.id.firstName, R.id.lastName, R.id.title});
setListAdapter(adapter);
}
}
错误,堆栈跟踪:
shortMsg:java.lang.NullPointerException
longMsg:java.lang.NullPointerException: Unable to start activity ComponentInfo{com.simple.search/com.simple.search.SimpleSearch}: java.lang.NullPointerException
stackTrace:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simple.search/com.simple.search.SimpleSearch}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1664)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3703)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.simple.search.SimpleSearch.onCreate(SimpleSearch.java:34)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1628)
... 11 more
数据库助手
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "test_diectory";
protected Context context;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String s;
try {
Toast.makeText(context, "1", 2000).show();
InputStream in = context.getResources().openRawResource(R.raw.sql);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in, null);
NodeList statements = doc.getElementsByTagName("statement");
for (int i=0; i<statements.getLength(); i++) {
s = statements.item(i).getChildNodes().item(0).getNodeValue();
db.execSQL(s);
}
} catch (Throwable t) {
Toast.makeText(context, t.toString(), 50000).show();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS employees");
onCreate(db);
}
}
public类DatabaseHelper扩展了SQLiteOpenHelper{
公共静态最终字符串数据库\u NAME=“test\u diectory”;
受保护的语境;
公共数据库助手(上下文){
super(上下文,数据库名称,null,1);
this.context=上下文;
}
@凌驾
public void onCreate(SQLiteDatabase db){
字符串s;
试一试{
Toast.makeText(上下文,“1”,2000).show();
InputStream in=context.getResources().openRawResource(R.raw.sql);
DocumentBuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc=builder.parse(in,空);
节点列表语句=doc.getElementsByTagName(“语句”);
对于(int i=0;i多亏了adil。看起来我忘记了初始化db。添加db=(新DatabaseHelper(this))。getWritableDatabase();
修复了错误。你的光标是什么getCount()?调试它。。你能解释一下怎么做吗?我是eclipse和android新手。第34行是doMySearch(查询);那么您的db
在哪里声明和初始化?请参阅,在您的SimpleSearch
中,您有一个受保护的SQLiteDatabase db;
您在哪里初始化它。。