Android Stack NullPointer在Search Widget中选择建议

时间:2016-01-16 14:18:53

标签: 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;
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

首先,您的代码上的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中的内容,但可以肯定它是由空值引起的。