Android Search suggestions cursor at row X returned exception

1.4k views Asked by At

I have been trying to get this piece of code to work but with no joy. Here is the error from logcat

Search suggestions cursor at row 0 returned exception.
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.app.SearchableInfo.getSuggestIntentAction()' on a null object reference
            at android.support.v7.widget.SearchView.createIntentFromSuggestion(SearchView.java:1541)
            at android.support.v7.widget.SearchView.launchSuggestion(SearchView.java:1357)
            at android.support.v7.widget.SearchView.onItemClicked(SearchView.java:1270)
            at android.support.v7.widget.SearchView.access$1900(SearchView.java:104)
            at android.support.v7.widget.SearchView$10.onItemClick(SearchView.java:1294)
            at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:905)
            at android.widget.AutoCompleteTextView.access$500(AutoCompleteTextView.java:90)
            at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1195)
            at android.widget.AdapterView.performItemClick(AdapterView.java:300)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1143)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:3044)
            at android.widget.AbsListView$3.run(AbsListView.java:3833)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

The annoying thing is it sort of works. I am able to see the results displayed but when I click on the results (i have no onclicklistener) i get the above error.

package com.overdrivedx.arep;

import android.app.SearchManager;
import android.content.Context;
import android.database.MatrixCursor;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.overdrivedx.adapter.SearchAdapter;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends ActionBarActivity {
    private List<String> items;
    private Menu menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        items = new ArrayList<String>();
        items.add("Lagos");
        items.add("Yahoo");
        items.add("Gooogle");
        items.add("OK! Stupid");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        this.menu = menu;

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView search = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        search.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        //search.setIconifiedByDefault(true); // Do not iconify the widget; expand it by default

        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                loadData(s);
                return false;

            }

        });
        return true;
    }

    private void loadData(String q) {
            String[] columns = new String[] { "_id", "text" };
            Object[] temp = new Object[] { 0, "default" };

            MatrixCursor cursor = new MatrixCursor(columns);

            ArrayList <String> listClone = new ArrayList<String>();

            for (String string : items) {

                if(string.matches("(?i)(" + q +").*")){
                    listClone.add(string);
                }
            }

            if(listClone.size() < 1) {
                listClone.add("No results");
            }

            for(int i = 0; i < listClone.size(); i++) {
                temp[0] = i;
                temp[1] = listClone.get(i);
                Log.v("input", listClone.get(i));
                cursor.addRow(temp);
            }

        MenuItem searchItem = menu.findItem(R.id.menu_search);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView) MenuItemCompat.getActionView(searchItem);

        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
        searchView.setSuggestionsAdapter(new SearchAdapter(this, cursor, listClone));

}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

I also have a SearchAdapter Class.

package com.overdrivedx.adapter;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.overdrivedx.chatarep.R;
import java.util.List;

public class SearchAdapter extends CursorAdapter {

    private List items;
    private TextView text;

    public SearchAdapter(Context context, Cursor cursor, List items) {
        super(context, cursor, false);
        this.items = items;

    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // Show list item data from cursor
        text.setText((CharSequence) items.get(cursor.getPosition()));
        // Alternatively show data direct from database
        //text.setText(cursor.getString(cursor.getColumnIndex("column_name")));

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.activity_search_results, parent, false);
        text = (TextView) view.findViewById(R.id.text);
        return view;
    }

}
2

There are 2 answers

1
banachi On

I have exactly the same problem. I implemented the following and its working for me.

Follow the instruction on

http://developer.android.com/guide/topics/search/search-dialog.html

to create the following.

  1. Add searchable.xml in res/xml directory.

    http://developer.android.com/guide/topics/search/search-dialog.html#SearchableConfiguration

  2. Update manifest to include

    http://developer.android.com/guide/topics/search/search-dialog.html#DeclaringSearchableActivity

0
Brian W. Pham On

This problem also drove me nuts for 2 days. What's happening is that after you've clicked on a result, your app is expecting that an operation has not completed and is trying to go further into an Intent argument.

The easiest way, and how I solved it, was to have the activity implement SearchView.OnSuggestionListener and return true in the overridden method for onSuggestionClick(int p), which notifies your app that the click operation has completed.