我将此片段与 menu.xml 连接。我单击它时菜单打开,除了 onOptionsItemSelected 之外,一切都有效。我试图记录它,但没有结果。为什么我的代码没有工作?
活动
@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"/>
答案 0 :(得分:1)
你没有给菜单充气
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
}