Null pointer in SearchView AppCompatActivity?

I am just a beginner and I searched for this question, found 4-5 questions here but it didn't work for me. I am just trying to searchView in Actionbar, my Activity is AppCompatActivity, my code is below. I am using the toolbar in this exercise. and using this library . OncreatOptionMenu:

 @Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    SearchManager SManager =  (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    SearchView searchViewAction = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
    searchViewAction.setSearchableInfo(SManager.getSearchableInfo(getComponentName()));
    searchViewAction.setIconifiedByDefault(false);

    return super.onCreateOptionsMenu(menu);
}

      

Xml:

<item android:id="@+id/action_search"
    android:title="action_search"
    MainActivity:showAsAction="ifRoom"
    MainActivity:actionViewClass="android.support.v7.widget.SearchView" />

      

and logcat:

java.lang.NullPointerException
        at algonation.com.spm.MainActivity.onCreateOptionsMenu(MainActivity.java:183)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2571)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:277)
        at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:84)
        at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImplBase.java:251)
        at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:84)
        at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:449)
        at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:66)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5292)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
        at dalvik.system.NativeStart.main(Native Method)

      

If you need all the MainActivity code, please let me know .. Thank you so much.

+3


source to share


3 answers


You are getting NullPointerException because below line is returning null.

SManager.getSearchableInfo (getComponentName ()

You need

1 - Declare an activity (which should perform a search) to accept the ACTION_SEARCH intent in the target-filter element .

2 - Specify the search configuration to be used in the metadata item .

Assuming you are searching in MainActivity (which is AppCompatActivity as you want), see below example :

public class MainActivity extends AppCompatActivity {

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


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

    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    SearchView searchViewAction = (SearchView) MenuItemCompat
            .getActionView(searchMenuItem);

    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchableInfo searchableInfo = searchManager 
            .getSearchableInfo(getComponentName());
    searchViewAction.setSearchableInfo(searchableInfo);
    searchViewAction.setIconifiedByDefault(false);

    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.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

      

AndroidManifest.xml



 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

          <intent-filter>
             <action android:name="android.intent.action.SEARCH" />
          </intent-filter>

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

    </activity>

</application>

      

menu / activity _main.xml

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

      

Res /xml/searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_label"
    android:hint="@string/search_hint" >
</searchable>

      

Hope this solves your problem and see here for help .

+5


source


This thread is half old, but the answers didn't work for me. For me this changed the "app" to "appcompat".

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto"
<item
    android:id="@+id/action_search"
    android:title="@string/search_title"
    appcompat:actionViewClass="android.support.v7.widget.SearchView"
    appcompat:showAsAction="always"/>
</menu>

      



Excuse my english.

+2


source


Actually, just make sure you import "android.support.v7.widget.SearchView" and NOT "android.widget.SearchView"

import android.support.v7.widget.SearchView

      

+1


source







All Articles