0

I want to use custom SearchView in menu of my App but I'm encountering a NullPointerException in android while using actionLayout in menu.xml I have custom layout for menu :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
    android:id="@+id/search_btn"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@android:drawable/ic_menu_search"/>

<EditText
    android:id="@+id/search_et"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/search_btn"
    android:layout_toLeftOf="@+id/search_btn"
    android:ems="10"
    android:inputType="none" >

    <requestFocus />
</EditText>

and my menu.xml is :

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/search_view"        
    android:icon="@android:drawable/ic_menu_search"
    android:actionLayout="@layout/search_menu"
    android:showAsAction="collapseActionView|ifRoom"
    android:title="@string/search_title"/>
</menu>

Now I want to add OnClickListener on _search_btn_ So I did that likewise :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);

    searchButton = (Button) menu.findItem(R.id.search_btn);

    searchButton.setOnClickListener(new OnClickListener() { // SEE HERE I'M GETTING NullPointerException 
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, ((EditText) findViewById(R.id.search_et)).getText().toString(), Toast.LENGTH_LONG).show();

        }
    });
    return true;
}

but I'm getting my NullPointerException on above mentioned line. How can I add ClickListener to that button???

3 Answers3

3

You're using the wrong id. Use the id for your menu item. You can use getActionView on the MenuItem to get the layout.

@Override
 public boolean onCreateOptionsMenu(Menu menu) {
     getMenuInflater().inflate(R.menu.menu, menu);

    MenuItem searchItem = menu.findItem(R.id.search_view);
    Button searchButton = searchItem.getActionView().findViewById(R.id.search_btn);
    searchButton.setOnClickListener(new OnClickListener() { // SEE HERE I'M GETTING    NullPointerException 
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, ((EditText)    findViewById(R.id.search_et)).getText().toString(), Toast.LENGTH_LONG).show();

        }
    });
   return true;
}
sihrc
  • 2,728
  • 2
  • 22
  • 43
  • Your answer is very helpful! Even in case of using NavigationView we can do that (in onCreate() method instead of onCreateOptionsMenu()). We have to only get reference to MenuItem in the other way: `NavigationView navigationView = this.findViewById(R.id.navigation_view); MenuItem searchItem = navigation.getMenu().findItem(R.id.search_view); //... ` – Anad May 11 '18 at 08:25
1

please try on onOptionsItemSelected method.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.search_view:
        //write your code here
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}
bhavesh kaila
  • 761
  • 1
  • 5
  • 25
0

Well your not getting a reference to your button. i.e in searchButton = (Button) menu.findItem(R.id.search_btn); you searchButton is null.

Your using a wrong id.

Oliver
  • 6,152
  • 2
  • 42
  • 75