我正在尝试将SearchableDictionary示例合并到我的应用程序中,以获得简单的词汇表搜索功能。如果您不熟悉SearchableDictionary,请单击菜单中的搜索图标并输入搜索词。它根据您的搜索建议项目,如果您单击go或键盘输入,它将提取与搜索匹配的项目列表。选择项目将显示全屏定义。
我已经解决了这个问题,它只有一个例外,当我在程序强制关闭的几个字母后按下或进入键盘时,并没有显示匹配条件的列表。所有其他方面都有效,它建议在输入术语时单击项目并单击某个项目将显示其定义。
这是错误发生时的logcat。我发现它发生在SearchableDictionary.java中,但无法查明错误。
tl; dr - 尝试列出可能的搜索匹配时,应用程序崩溃。
10-12 02:35:41.450: E/AndroidRuntime(524): FATAL EXCEPTION: main
10-12 02:35:41.450: E/AndroidRuntime(524): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.appname/com.company.appname.SearchableDictionary}: java.lang.NullPointerException
10-12 02:35:41.450: E/AndroidRuntime(524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
10-12 02:35:41.450: E/AndroidRuntime(524): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
10-12 02:35:41.450: E/AndroidRuntime(524): at android.app.ActivityThread.access$600(ActivityThread.java:122)
10-12 02:35:41.450: E/AndroidRuntime(524): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
10-12 02:35:41.450: E/AndroidRuntime(524): at android.os.Handler.dispatchMessage(Handler.java:99)
10-12 02:35:41.450: E/AndroidRuntime(524): at android.os.Looper.loop(Looper.java:137)
10-12 02:35:41.450: E/AndroidRuntime(524): at android.app.ActivityThread.main(ActivityThread.java:4340)
10-12 02:35:41.450: E/AndroidRuntime(524): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 02:35:41.450: E/AndroidRuntime(524): at java.lang.reflect.Method.invoke(Method.java:511)
10-12 02:35:41.450: E/AndroidRuntime(524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-12 02:35:41.450: E/AndroidRuntime(524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-12 02:35:41.450: E/AndroidRuntime(524): at dalvik.system.NativeStart.main(Native Method)
10-12 02:35:41.450: E/AndroidRuntime(524): Caused by: java.lang.NullPointerException
10-12 02:35:41.450: E/AndroidRuntime(524): at com.company.appname.SearchableDictionary.showResults(SearchableDictionary.java:112)
10-12 02:35:41.450: E/AndroidRuntime(524): at com.company.appname.SearchableDictionary.handleIntent(SearchableDictionary.java:78)
10-12 02:35:41.450: E/AndroidRuntime(524): at com.company.appname.SearchableDictionary.onCreate(SearchableDictionary.java:56)
10-12 02:35:41.450: E/AndroidRuntime(524): at android.app.Activity.performCreate(Activity.java:4465)
10-12 02:35:41.450: E/AndroidRuntime(524): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-12 02:35:41.450: E/AndroidRuntime(524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
10-12 02:35:41.450: E/AndroidRuntime(524): ... 11 more
此外,这里是完整的SearchableDictionary.java:
public class SearchableDictionary extends Activity {
private TextView mTextView;
private ListView mListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
mTextView = (TextView) findViewById(R.id.text);
mListView = (ListView) findViewById(R.id.list);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
// Because this activity has set launchMode="singleTop", the system calls this method
// to deliver the intent if this actvity is currently the foreground activity when
// invoked again (when the user executes a search from this activity, we don't create
// a new instance of this activity, so the system delivers the search intent here)
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
// handles a click on a search suggestion; launches activity to show word
Intent wordIntent = new Intent(this, WordActivity.class);
wordIntent.setData(intent.getData());
startActivity(wordIntent);
finish();
} else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
// handles a search query
String query = intent.getStringExtra(SearchManager.QUERY);
showResults(query);
}
}
/**
* Searches the dictionary and displays results for the given query.
* @param query The search query
*/
private void showResults(String query) {
Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, null,
new String[] {query}, null);
if (cursor == null) {
// There are no results
mTextView.setText(getString(R.string.no_results, new Object[] {query}));
} else {
// Display the number of results
int count = cursor.getCount();
String countString = getResources().getQuantityString(R.plurals.search_results,
count, new Object[] {count, query});
mTextView.setText(countString);
// Specify the columns we want to display in the result
String[] from = new String[] { DictionaryDatabase.KEY_WORD,
DictionaryDatabase.KEY_DEFINITION };
// Specify the corresponding layout elements where we want the columns to go
int[] to = new int[] { R.id.word,
R.id.definition };
// Create a simple cursor adapter for the definitions and apply them to the ListView
SimpleCursorAdapter words = new SimpleCursorAdapter(this,
R.layout.result, cursor, from, to);
mListView.setAdapter(words);
// Define the on-click listener for the list items
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Build the Intent used to open WordActivity with a specific word Uri
Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class);
Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI,
String.valueOf(id));
wordIntent.setData(data);
startActivity(wordIntent);
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.search) {
onSearchRequested();
} else if (itemId == R.id.atcMainSearch) {
Intent atcGoHome = new Intent(SearchableDictionary.this,
mymainactivity.class);
startActivity(atcGoHome);
} else if (itemId == R.id.atcHelpSearch) {
Intent atcAboutWeb = new Intent(SearchableDictionary.this,
atcAboutWeb.class);
startActivity(atcAboutWeb);
}
return true;
}
}
感谢任何帮助过的人!
答案 0 :(得分:0)
一个简单且被忽视的错误导致了这个问题。在尝试合并此功能时,我没有使用所有布局,而是试图引用一个不存在的布局。将布局添加到我的项目后,应用程序将按预期运行。