119

Umieściłem setHasOptionsMenu(true) wewnątrz onCreateView, ale nadal nie mogę wywoływać wewnętrznych fragmentów onCreateOptionsMenu.onCreateOptionsMenu inside Fragments

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    setHasOptionsMenu(true);    
    return inflater.inflate(R.layout.facesheet, container, false); 
} 

Poniżej znajduje się mój kod onCreateOptionsMenu.

@Override 
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) { 
    getSupportMenuInflater().inflate(R.menu.layout, menu); 
    return (super.onCreateOptionsMenu(menu)); 
} 

Komunikat o błędzie pojawia się:

Sposób onCreateOptionsMenu(Menu) typu Fragment musi zastąpić lub zaimplementować metodę supertypem.

+0

Dzięki za 'setHasOptionsMenu (true);', szukałem tego. –

Odpowiedz

365

spróbować,

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    inflater.inflate(R.menu.menu_sample, menu); 
    super.onCreateOptionsMenu(menu,inflater); 
} 

I onCreate dodać tę linię, aby opcje pojawiają się w Toolbar

setHasOptionsMenu(true); 
+1

Z jakiegoś powodu funkcja onCreateOptionsMenu nie jest wywoływana w moim fragmencie –

+149

, nie zostanie wywołana, jeśli nie dodasz tej linii: 'setHasOptionsMenu (true);' –

+9

onCreateOptionsMenu() dla fragmentów ma inne argumenty niż dla działań. – Jorge

18

Twój już masz plik wygenerowany automatycznie RES/menu /menu.xml definiowanie action_settings.

W swojej MainActivity.java mają następujące metody:

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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    switch (id) { 
     case R.id.action_settings: 
      // do stuff, like showing settings fragment 
      return true; 
    } 

    return super.onOptionsItemSelected(item); // important line 
} 

w metodzie onCreateView() swojej rozmowy fragment:

setHasOptionsMenu(true); 

a także dodać te 2 metod:

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    inflater.inflate(R.menu.fragment_menu, menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    switch (id) { 
     case R.id.action_1: 
      // do stuff 
      return true; 

     case R.id.action_2: 
      // do more stuff 
      return true; 
    } 

    return false; 
} 

Wreszcie dodaj nowy plik res/menu/fragment_menu.xml definiowanie action_1 i action_2.

W ten sposób, gdy aplikacja wyświetla fragment jego menu zawiera 3 pozycje:

  • action_1 z res/menu/fragment_menu.xml
  • action_2 z RES/menu /fragment_menu.xml
  • action_settings z rES/menu /menu.xml
+0

pytanie było wewnątrz fragmentów, nie aktywności – OlivierM

+1

@OlivierM odpowiedź wyjaśnia menu fragmentów. Doceniam wyjaśnienie, w jaki sposób współdziała z menu działań. – Aranda

5

Próbowałem odpowiedzi @Alexander Farber i @Sino Raj.Obie odpowiedzi są ładne, ale nie mogłem użyć onCreateOptionsMenu wewnątrz mojego fragmentu, aż odkryłem czego brakuje:

Dodaj setSupportActionBar (pasek narzędzi) w mojej działalności, tak:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.id.activity_main); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
} 

Mam nadzieję, że odpowiedź może być pomocna dla kogoś z tym samym problemem.

0
@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.activity_add_customer, container, false); 
     setHasOptionsMenu(true); 
} 

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    inflater.inflate(R.menu.menu_sample, menu); 
    super.onCreateOptionsMenu(menu,inflater); 
} 
0

setSupportActionBar(toolbar) połączeń wewnątrz onViewCreated(...) fragmentu @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ((MainActivity)getActivity()).setSupportActionBar(toolbar); setHasOptionsMenu(true); }

Powiązane problemy