我想給SearchableDictionary樣品納入我的應用程序有一個簡單的詞彙搜索能力。如果您不熟悉SearchableDictionary,請單擊菜單中的搜索圖標並輸入搜索詞。它建議根據您的搜索項目,如果你點擊進入或者在鍵盤上輸入這將拉動匹配搜索項的列表。選擇一個項目將顯示全屏定義。Android的 - 結合SerachableDictionary樣品到我的應用程序
我已經incorperated這一點,它有一個例外的作品,當我打去還是之後只有幾個字母在鍵盤上輸入程序強制關閉,並不會帶來了匹配項的列表。所有其他方面的工作,它建議在輸入術語和點擊一個項目的項目將提出其定義。
下面是當錯誤發生的logcat的。我發現它發生在SearchableDictionary.java中,但無法查明錯誤。
TL;博士 - 應用程序試圖列出可能的搜索匹配時崩潰。
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;
}
}
謝謝你的人誰可以幫助!