如何在我的Android应用程序中添加选项菜单?

时间:2015-10-04 09:22:41

标签: android android-activity

我想在我的Android应用中添加一个选项菜单。但我不知道当我运行强制关闭时我有什么问题,当我搜索我的日志有很多错误,我不明白。我正在学习机器人,所以我的经历很糟糕。

请查看我的代码并告诉我我的错误?

   import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
     public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.item1:
          Toast.makeText(this, "Option1", Toast.LENGTH_SHORT).show();
          return true;
          break;
        case R.id.item2:
          Toast.makeText(this, "Option2", Toast.LENGTH_SHORT).show();
          return true;
          break;
        case R.id.item3:
          Toast.makeText(this, "Option3", Toast.LENGTH_SHORT).show();
          return true; 
          break;          
        case R.id.item4:
          Toast.makeText(this, "Option4", Toast.LENGTH_SHORT).show();
          return true;
          break;
        case R.id.item5:
          Toast.makeText(this, "Option5", Toast.LENGTH_SHORT).show();
          return true;
          break;
        case R.id.item6:
          Toast.makeText(this, "Option6", Toast.LENGTH_SHORT).show();
          return true; 
          break;
        default:
          return super.onOptionsItemSelected(item);
        } 
    } 
}

1 个答案:

答案 0 :(得分:0)

使用此功能可帮助您删除中断;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
     public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.item1:
          Toast.makeText(this, "Option1", Toast.LENGTH_SHORT).show();
          return true;
        case R.id.item2:
          Toast.makeText(this, "Option2", Toast.LENGTH_SHORT).show();
          return true;
        case R.id.item3:
          Toast.makeText(this, "Option3", Toast.LENGTH_SHORT).show();
          return true;           
        case R.id.item4:
          Toast.makeText(this, "Option4", Toast.LENGTH_SHORT).show();
          return true;
        case R.id.item5:
          Toast.makeText(this, "Option5", Toast.LENGTH_SHORT).show();
          return true;
        case R.id.item6:
          Toast.makeText(this, "Option6", Toast.LENGTH_SHORT).show();
          return true;           
        default:
          return super.onOptionsItemSelected(item);
        } 
    } 
}