每次在溢出选项菜单中打开时复制菜单项

Duplicating menu items everytime it open in overflow options 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/res-auto">

<item
android:id="@+id/search"
android:title="@string/search"
android:icon="@drawable/ic_action_search"
app:showAsAction="ifRoom"
/>
<item
android:id="@+id/share"
android:title="@string/share"
android:icon="@drawable/ic_action_share"
app:showAsAction="ifRoom"
/>

我的 java 代码是:

    package com.erprakash.contacts;
    import android.content.ContentUris;
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    //import android.net.Uri;
    import android.provider.ContactsContract;
    //import android.support.v7.app.ActionBar;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Toast;

    import java.util.ArrayList;
    //import java.util.Comparator;

   public class MainActivity extends AppCompatActivity {

   private RecyclerView recyclerView;
   private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;

ArrayList<Contact> arrayList = new ArrayList<>();
int img_id = R.drawable.image1;
String name ;
String number ;
private int id;
private Contact contact;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   //Tool bar setting
    //toolbar end
    // Reading Contacts -----Working
    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");
    if (phones != null) {
        while (phones.moveToNext())
        {
            name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            id = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO_FILE_ID));
            if(id == 0){
                contact = new Contact(number, name, img_id);
            }else {
                 contact = new Contact(number, name, id);
            }
            arrayList.add(contact);
        }
        phones.close();
    }



    /**
    name = getResources().getStringArray(R.array.person_name);
    number = getResources().getIntArray(R.array.mobile_number);
    int c = 0;
    for(String Name: name)
    {
        Contact contact = new Contact(number[c],Name,img_id[c]);
        c++;
        arrayList.add(contact);
    }
     **/
    recyclerView = (RecyclerView)findViewById(R.id.rvMainContennt);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    adapter = new ContactAdapter(arrayList);
    recyclerView.setAdapter(adapter);


}
//-------------Toolbar------------


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int res_id = item.getItemId();
    if(res_id == R.id.search)
    {
        Toast.makeText(this.getApplicationContext(),"You have seleted search option",Toast.LENGTH_SHORT).show();
    }
    return true;
}
@Override
public boolean onPrepareOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}
}

在预览中设计显示正确:

但处于 运行 状态:

然后单击选项菜单:

每次当我打开选项菜单时,内容都会重复并继续,我已经搜索了解决方案,但我没有在任何地方找到它。帮帮我....提前致谢

删除 onPrepareOptionsMenu().

@Override
public boolean onPrepareOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}

更新 onCreateOptionsMenu() 如下:

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

如果您想始终在 Toolbar 上显示菜单项,请更新 menu_main.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/res-auto">

    <item
        android:id="@+id/search"
        android:title="@string/search"
        android:icon="@drawable/ic_action_search"
        app:showAsAction="always" />

    <item
        android:id="@+id/share"
        android:title="@string/share"
        android:icon="@drawable/ic_action_share"
        app:showAsAction="always" />
</menu>