SearchableDictionary サンプルをアプリに組み込んで、簡単な用語集検索機能を持たせようとしています。SearchableDictionary に慣れていない場合は、メニューの検索アイコンをクリックして検索語を入力します。検索に基づいてアイテムを提案し、キーボードで [go] または [Enter] をクリックすると、検索に一致するアイテムのリストが表示されます。項目を選択すると、全画面定義が表示されます。
私はこれを組み込みましたが、1 つの例外を除いて機能します。ほんの数文字後にキーボードで [go] または [Enter] を押すと、プログラム強制が終了し、一致する用語のリストが表示されません。他のすべての側面は機能します。用語を入力してアイテムをクリックすると、その定義が表示されます。
エラーが発生したときのログキャットを次に示します。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;
}
}
助けてくれた人に感謝します!