我正在开发一个应用程序,它应该通过数据库中的查询进行膨胀和自动完成文本查看。我遇到的问题是,当我调用方法来膨胀TextView时,我无法访问数据库。
TextChanged Listener(取自CustomAutocompleteTextChangedListener.java):
@Override
public void onTextChanged(CharSequence userInput, int start, int before, int count) {
//Log user input
Log.d(TAG,"User input : " + userInput);
Vindsiden vindsiden = new Vindsiden();
//Query the database based on the user input
vindsiden.item = vindsiden.getItemsFromDb(userInput.toString());
//Update the adapter
vindsiden.myAdapter.notifyDataSetChanged();
vindsiden.myAdapter = new ArrayAdapter<String>(vindsiden, android.R.layout.simple_dropdown_item_1line,vindsiden.item);
vindsiden.AcTv_sok.setAdapter(vindsiden.myAdapter);
}
然后调用方法getItemsFromDb
public List<Sted> read(String searchTerm) {
List<Sted> recordList = new ArrayList<Sted>();
//Select query
String sql = "";
sql += "SELECT * FROM" + TABLE_PLACES;
sql += " WHERE " + COLUMN_NAME + " LIKE '%" + searchTerm + "%'";
sql += " ORDER BY " + COLUMN_ID + " DESC";
sql += " LIMIT 0,5";
//The line under is what is returning null
SQLiteDatabase database = this.getWritableDatabase();
//execute the query
Cursor cursor = database.rawQuery(sql, null);
//Looping rows and adding to the list
if (cursor.moveToFirst()) {
do {
String objectName = cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
Sted sted = new Sted();
recordList.add(sted);
} while (cursor.moveToNext());
}
cursor.close();
database.close();
return recordList;
}
我有几个访问数据库的项目,其他方法可以工作,例如下面的方法:
public Sted findPlace(String placeName) {
String query = "Select * FROM " + TABLE_PLACES + " WHERE " + COLUMN_NAME + " = \"" + placeName + "\"";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(query, null);
Sted sted = new Sted();
if (cursor.moveToFirst()) {
cursor.moveToFirst();
sted.set_id(Integer.parseInt(cursor.getString(0)));
sted.set_stedsNavn(cursor.getString(1));
sted.set_url(cursor.getString(2));
cursor.close();
} else {
sted = null;
}
database.close();
return sted;
}
Stacktrace:
02-01 10:50:08.201 14164-14164/com.vindsiden.windwidget E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.vindsiden.windwidget, PID: 14164
java.lang.NullPointerException
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:256)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.vindsiden.windwidget.config.DatabaseHandler.read(DatabaseHandler.java:143)
at com.vindsiden.windwidget.Vindsiden.getItemsFromDb(Vindsiden.java:483)
at com.vindsiden.windwidget.config.CustomAutocompleteTextChangedListener.onTextChanged(CustomAutocompleteTextChangedListener.java:38)
at android.widget.TextView.sendOnTextChanged(TextView.java:7408)
at android.widget.TextView.handleTextChanged(TextView.java:7467)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9183)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:675)
at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:437)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:333)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
完整代码可用于: https://github.com/danielgolan/Vindsiden/tree/Daniel2
答案 0 :(得分:0)
您传递给Context
父构造函数的SQLiteOpenHelper
为null
。