I have a simple activity
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_search_city);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_action, menu);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
}
}
with this menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/tool"
app:context=".MainActivity">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="Search City"
app:actionviewclass="android.support.v7.widget.SearchView"
app:showasaction="always">
</item>
</menu>
(Note that if I use res-auto instead of tool, the project doesn't build saying showasaction and actionviewclass are not found...)
with this style.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"></style>
</resources>
But when I start the app I get a null pointer because MenuItemCompat.getActionView returns null..
Could someone point me where is the mistake?
ps: I have this is my gradle compile 'com.android.support:appcompat-v7:23.0.1'
thank you!
Two problems:
res-auto
- the tool
namespace set gets completely stripped before inclusion in your APKapp:actionViewClass
and app:showAsAction
as per the Action Bar training