[英]Android onCreateOptionsMenu is never called so the menu isn't being displayed
I want to have a search bar in my menu toolbar, but when I navigate to the activity, the menu is never created because onCreateOptionsMenu is never called. 我想在菜单工具栏中有一个搜索栏,但是当我导航到活动时,由于从未调用过onCreateOptionsMenu,所以从未创建菜单。 I'm extending AppCompatActivity
我正在扩展AppCompatActivity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, 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;
}
Menu: 菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
android:title="@string/search"
android:icon="@drawable/ic_search_black_36dp"
app:showAsAction ="ifRoom|collapseActionView"
app:actionViewClass ="android.support.v7.widget.SearchView" />
</menu>
The problem is probably on your AndroidManisfest.xml
file. 问题可能出在您的
AndroidManisfest.xml
文件上。 With the following default code I get the onCreateOptionsMenu(Menu menu)
method loaded: 使用以下默认代码,我加载了
onCreateOptionsMenu(Menu menu)
方法:
AndroidManisfest.xml: AndroidManisfest.xml:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity.java: MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d("BugFix","nothing");
getMenuInflater().inflate(R.menu.menu_search, 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;
}
}
activity_main.xml: activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.el_greeko.exampleproject.MainActivity">
</android.support.constraint.ConstraintLayout>
And the rest is your information given, with menu.search.xml in res/menu/ 剩下的就是您提供的信息,在res / menu /中包含menu.search.xml
Btw. 顺便说一句。 I suggest to read the instructions in the webpage of the Search dialog .
我建议阅读“ 搜索”对话框网页中的说明。 It may give you also a better solution for your search activity.
它也可以为您的搜索活动提供更好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.