Search code examples
androidsearchnullpointerexceptionappbar

Failed to implement a search widget


I'm trying to implement a search widget in my app . I found a useful tutorial from here.

My Activity A

But my app crashed.

Activity A

     @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.create_menu, menu);
            // Associate searchable configuration with the SearchView
            SearchManager searchManager =
                    (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchView searchView =
                    (SearchView) menu.findItem(R.id.search).getActionView();
            searchView.setSearchableInfo(
                    searchManager.getSearchableInfo(getComponentName()));
            return true;
        }

 @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.
        switch (item.getItemId()) {

            case R.id.add: // create new file
                View menuItemView = findViewById(R.id.add);
                PopupMenu po = new PopupMenu(HomePage.this, menuItemView); //for drop-down menu
                po.getMenuInflater().inflate(R.menu.popup_menu, po.getMenu());
                po.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        //  Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
                        if ("Create New File".equals(item.getTitle())) {
                            Intent intent = new Intent(HomePage.this, Information.class);  // go to Information class
                            startActivity(intent);

                        } else if ("Edit File".equals(item.getTitle())) {
                            Intent intent = new Intent(HomePage.this, Edit.class);
                            startActivity(intent);
                        }
                        return true;
                    }
                });
                po.show(); //showing popup menu

        }
        return super.onOptionsItemSelected(item);
    }

searchable in xml folder

<?xml version="1.0" encoding="utf-8"?>

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="Search by date" />

create_menu

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/search"
        android:title="Search by date"
        android:icon="@mipmap/search"
        app:showAsAction="collapseActionView|ifRoom"
        android:actionViewClass="android.widget.SearchView" />

    <item
        android:icon="@mipmap/menu"
        android:id="@+id/add"
        android:orderInCategory="100"
        android:title="Main Menu"
        app:showAsAction="always" />

</menu>

declare this in mainfest

  <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable" />

Error LogCat

01-18 18:31:09.298    9215-9215/com.example.project.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.project.myapplication, PID: 9215
    java.lang.NullPointerException
            at com.example.project.myapplication.GUI.HomePage.onCreateOptionsMenu(HomePage.java:104)
            at android.app.Activity.onCreatePanelMenu(Activity.java:2646)
            at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:298)
            at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallback

This is the line where error pointed to searchView.setSearchableInfo(


Solution

  • You should use support SearchView's support version:

    <item android:id="@+id/action_search"
        android:title="@string/search_title"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="ifRoom"
        app:actionViewClass="android.support.v7.widget.SearchView" />
    

    And you'd add in Manifest.xml the proper Intent Filter

            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
    
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />