Android OnCreateOptions菜单从未被调用,因此菜单为';不显示

Android OnCreateOptions菜单从未被调用,因此菜单为';不显示,android,menu,android-appcompat,android-search,Android,Menu,Android Appcompat,Android Search,我想在我的菜单工具栏中有一个搜索栏,但当我导航到活动时,菜单永远不会被创建,因为OnCreateOptions菜单永远不会被调用。我在延长活动时间 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_search, menu); final SearchView searchView = (SearchView) MenuItemCompat

我想在我的菜单工具栏中有一个搜索栏,但当我导航到活动时,菜单永远不会被创建,因为OnCreateOptions菜单永远不会被调用。我在延长活动时间

@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;
}
菜单:


问题可能出在您的
AndroidManisfest.xml
文件上。使用以下默认代码,我将加载
onCreateOptions菜单(菜单菜单)
方法:

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>
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>


. 它还可以为您的搜索活动提供更好的解决方案。

@Pavneet_Singh我也尝试过,但它不起作用。那么您应该发布完整的代码,以便周围的人可以测试它。我认为您使用的是toobar,而您没有将其设置为主要功能actionBar@OussemaAroua是的,那是我的问题!非常感谢:)我很感谢你花时间写下答案,但乌塞马发现了我的问题。我只需要将工具栏设置为主操作栏
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;
    }
}
<?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>