Reputation:
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???
Upvotes: 0
Views: 989
Reputation: 761
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);
}
}
Upvotes: 1
Reputation: 6250
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.
Upvotes: 0
Reputation: 2828
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;
}
Upvotes: 3