1

我正在尝试将 SearchableDictionary 示例合并到我的应用程序中,以获得简单的词汇表搜索功能。如果您不熟悉 SearchableDictionary,请单击菜单中的搜索图标并输入您的搜索词。它会根据您的搜索建议项目,如果您单击 go 或在键盘上输入,它将拉出与搜索匹配的项目列表。选择一个项目将显示一个全屏定义。

我已经合并了这一点,它有一个例外,当我在几个字母后点击 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;
}

}

感谢任何帮助的人!

4

1 回答 1

0

一个简单且被忽视的错误导致了这个问题。在尝试合并此功能时,我没有使用所有布局,而是尝试引用不存在的布局。将布局添加到我的项目后,应用程序按预期运行。

于 2012-10-12T22:57:20.603 回答