新手刷《第一行代码》2.2.6中在活动中添加menu出现错误:需要class,interface或enum


刷《第一行代码》2.2.6中在活动中添加menu
FirstActivity中,重写onCreateOptionsMenu()和OnOptionsItemSelected()方法:


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

public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) {
    case R.id.add_item:
        Toast.makeText(this, "You clicked Add", Toast.LENGTH_SHORT).show();
        break;
    case R.id.remove_item:
        Toast.makeText(this, "You clicked Remove", Toast.LENGTH_SHORT).show();
        break;
    default:
    }
    return true;
}

出现错误:需要class,interface或enum
clipboard.png

Android java android-studio

Naaru 9 years, 2 months ago

需要将代码移到 public class FirstActivity extends Activity{ 你的代码} 里面。

路过的童鞋 answered 9 years, 2 months ago

Your Answer