Hi everyone,
I am trying to implement a local applications search for my Android app and I'm having difficulties.
I've set up my searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint" >
</searchable>
I've set up my AndroidManifest:
<activity android:name=".SearchActivity"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity ...></activity>
<meta-data android:name="android.app.default_searchable"
android:value=".SearchActivity" />
My SearchActivity:
public class SearchActivity extends ListActivity {
private mDbAdapter mDbHelper = new mDbAdapter(this);
private TextView queryText;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.search_screen);
queryText = (TextView)findViewById(R.id.queryString);
final Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
showResults(query);
}
}
private void showResults(String query) {
queryText.setText("\"" + query + "\"");
Cursor c = mDbHelper.getSearchMatches(query);
String[] from = new String[]{mDbAdapter.KEY_NAME, mDbAdapter.KEY_CATEGORIES};
int[] to = new int[]{R.id.listName, R.id.listCats};
SimpleCursorAdapter results = new SimpleCursorAdapter(this, R.layout.list_row, c, from, to);
setListAdapter(results);
}
And finally, my query:
public Cursor getSearchMatches(String query) {
String sql =
"SELECT * FROM " + DB_TABLE_DATA + " WHERE " + KEY_NAME + " LIKE '%" + query + "%'";
return mDb.rawQuery(sql, null);
}
With a series of Logs, I find that the application fails when it runs the rawQuery. I get a "java.lang.NullPointerException" error. However, when I run this same rawQuery in any other scenario it works perfectly. It just seems to be this Search intent. I don't find the need to use a Content Provider because I don't want to include the data in global search, just local. Anyone have any suggestions?
Thanks in advance! nickfm