Question

I have this fragment connected with a menu.xml. I the menu opens when i click it, everything works besides the onOptionsItemSelected. I've tried to log it but nothing gives result. Why isn't my code working?

Activity

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Add your menu entries here
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId())
    {
    case R.id.clear:
        writetofile("0", "file1.txt");
        writetofile("0", "file2.txt");
        writetofile("0", "file3.txt");
    break;

    case R.id.add:
        writetofile("1", "file1.txt");
        writetofile("1", "file2.txt");
        writetofile("1", "file3.txt");
    break;
    }
    return true;
}

XML

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/clear"
    android:orderInCategory="100"
    android:title="Clean"/>
<item
    android:id="@+id/add"
    android:orderInCategory="100"
    android:title="Add"/>
Was it helpful?

Solution

You didn't inflate the menu

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu, menu);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top