java - Android Stack NullPointer 在搜索小部件中选择建议

标签 java android autosuggest

我正在使用主屏幕中自动完成的搜索小部件。自动完成功能工作正常,但当我选择任何建议时,应用程序会因奇怪的 NullPointerException 而崩溃。我花了足够的时间在谷歌上搜索它,但找不到任何线索。我粘贴下面的代码。

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        Log.e("Conversion", "OnceateOption menu started");
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);

        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();

       // This is sets our icon as search icon on title bar
       ImageView searchHintIcon = (ImageView) findViewById(searchView,
              "android:id/search_button");
       searchHintIcon.setImageResource(R.drawable.searchicon);

        // Set the hint text color
        int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(id);
        searchTextView.setTextColor(Color.BLACK);


        // Set hint in search widget
        searchView.setQueryHint("Enter Conversion");

        Log.e("Conversion", "Before setting auto suggestions");
        // Set the auto suggestions
        ArrayList<String> list = new ArrayList<String>(Arrays.asList(suggestionList));
        AutoSuggestionAdapter autoSuggestionAdapter = new AutoSuggestionAdapter(this,
              R.layout.autosuggestion_layout, list);
        //ArrayAdapter<String> autoSuggestionAdapter = new ArrayAdapter<String>(this,
          //      android.R.layout.simple_dropdown_item_1line, suggestionList);
        ((AutoCompleteTextView) searchTextView).setAdapter(autoSuggestionAdapter);
        ((AutoCompleteTextView) searchTextView).setThreshold(3);
        // Assumes current activity is the searchable activity
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(true); // Do not iconify the widget; expand it by default
        Log.e("Conversion", "Autosuggestions set");

        return true;
    }

菜单项是

<item
        android:id="@+id/menu_search"
        android:icon="@drawable/searchicon"
        android:title="@string/action_search"
        android:showAsAction="always"
        android:actionViewClass="android.widget.SearchView"
        />

日志中的异常是:

01-16 19:47:22.550 17995-17995/? E/AndroidRuntime: java.lang.NullPointerException
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at android.widget.SearchView.launchSuggestion(SearchView.java:1387)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at android.widget.SearchView.onItemClicked(SearchView.java:1303)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at android.widget.SearchView.access$1800(SearchView.java:92)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at android.widget.SearchView$9.onItemClick(SearchView.java:1327)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:931)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at android.widget.AutoCompleteTextView.access$400(AutoCompleteTextView.java:98)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1235)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at android.widget.AbsListView.performItemClick(AbsListView.java:1150)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at android.widget.ListView.performItemClick(ListView.java:4397)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2985)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at android.widget.AbsListView$1.run(AbsListView.java:3671)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:615)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:92)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:155)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5520)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1058)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
01-16 19:47:22.550 17995-17995/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
01-16 19:47:22.560 399-4972/? E/EmbeddedLogger: App crashed! Process: com.example.app.myapplication

但是当我检查android类代码时不可能获得NullPointer。

private Intent createIntent(String action, Uri data, String extraData, String query,
        int actionKey, String actionMsg) {
    // Now build the Intent
    Intent intent = new Intent(action);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    // We need CLEAR_TOP to avoid reusing an old task that has other activities
    // on top of the one we want. We don't want to do this in in-app search though,
    // as it can be destructive to the activity stack.
    if (data != null) {
        intent.setData(data);
    }
    intent.putExtra(SearchManager.USER_QUERY, mUserQuery);
    if (query != null) {
        intent.putExtra(SearchManager.QUERY, query);
    }
    if (extraData != null) { /**** LINE NUMBER 1387 NULL POINTER IN THIS LINE ******************/
        intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
    }
    if (mAppSearchData != null) {
        intent.putExtra(SearchManager.APP_DATA, mAppSearchData);
    }
    if (actionKey != KeyEvent.KEYCODE_UNKNOWN) {
        intent.putExtra(SearchManager.ACTION_KEY, actionKey);
        intent.putExtra(SearchManager.ACTION_MSG, actionMsg);
    }
    intent.setComponent(mSearchable.getSearchActivity());
    return intent;
}

有什么想法吗?

最佳答案

首先,您代码中的 SearchView 代码与您的测试设备版本不同,因此行号不匹配。正如日志所述,它应该发生在 launchSuggestion() 方法中,而不是您发布的 createIntent() 中。

问题不在于 Android 的框架代码,它在处理提供的建议列表时崩溃,可能遇到列表中的 null 数据。

从您的代码中,以下部分正在制作建议列表:

// Set the auto suggestions
ArrayList<String> list = new ArrayList<String>(Arrays.asList(suggestionList));
AutoSuggestionAdapter autoSuggestionAdapter = new AutoSuggestionAdapter(this,
    R.layout.autosuggestion_layout, list);

在这里,适配器从 suggestionList 获取数据,但如果其中一个元素为 null,则可能导致 NullPointerException

我不知道suggestionList中的内容,但可以确定它是由其中的null引起的。

关于java - Android Stack NullPointer 在搜索小部件中选择建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34827979/

相关文章:

java - 使 Thymeleaf 为 :field consume Lombok-generated boolean getter

android - 是安卓:animateLayoutChanges supposed to work when a child view's height is updated?

search - 谷歌地图搜索输入中的建议列表

autosuggest - solr 5.3 - 建议功能不起作用

java - 可以在提交/回滚时调用 hibernate 中开始事务

java - 从 Activity 向上导航到 fragment 打开相同的 fragment - Android 导航组件

android - 未处理的异常 : MissingPluginException(No implementation found

search - 如何让 Solr suggester 也处理拼写错误?

java - 如何在没有任何关联实例的情况下调用构造函数?

java - 尝试用数据填充 View 时 AsyncLayoutInflater 崩溃