Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 新活动的菜单项_Java_Android_Menu_Android Activity - Fatal编程技术网

Java 新活动的菜单项

Java 新活动的菜单项,java,android,menu,android-activity,Java,Android,Menu,Android Activity,我正在尝试使用菜单按钮将用户发送到新活动,然后再返回。把它放在上下文中,我有一个基本的计算器,它在应用程序打开时运行(现在工作正常),然后我希望用户点击他/她的手机上的菜单按钮,(两个按钮应该弹出Home和Tip calculator),当用户点击Tip calculator时,它应该加载新的activity TipCalculator。然后,如果再次按下菜单按钮,则应显示相同的两个选项,并且主页按钮应将它们带回基本计算器 这是我的Calculator.java代码 @Override

我正在尝试使用菜单按钮将用户发送到新活动,然后再返回。把它放在上下文中,我有一个基本的计算器,它在应用程序打开时运行(现在工作正常),然后我希望用户点击他/她的手机上的菜单按钮,(两个按钮应该弹出Home和Tip calculator),当用户点击Tip calculator时,它应该加载新的activity TipCalculator。然后,如果再次按下菜单按钮,则应显示相同的两个选项,并且主页按钮应将它们带回基本计算器

这是我的Calculator.java代码

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_calculator, menu);

        menu.add(Menu.NONE,HOME_MENU_ID,Menu.NONE,"Home");
        menu.add(Menu.NONE,SECOND_MENU_ID,Menu.NONE,"Tip Calculator");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch(item.getItemId()){

        case HOME_MENU_ID:
            Intent main = new Intent(this,Calculator.class);
            startActivity(main);
            finish();
        return true;

        case SECOND_MENU_ID:
            Intent second = new Intent(this,TipCalculator.class);
            startActivity(second);
            finish();
        return true;

        }

        return super.onOptionsItemSelected(item);
    }
    public class TipCalculator extends Calculator{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tipcalculator);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_tipcalculator, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    Intent main = new Intent(this,Calculator.class);
    startActivity(main);
    finish();
    return super.onOptionsItemSelected(item);
}

}
这是TipCalculator.java的代码

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_calculator, menu);

        menu.add(Menu.NONE,HOME_MENU_ID,Menu.NONE,"Home");
        menu.add(Menu.NONE,SECOND_MENU_ID,Menu.NONE,"Tip Calculator");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch(item.getItemId()){

        case HOME_MENU_ID:
            Intent main = new Intent(this,Calculator.class);
            startActivity(main);
            finish();
        return true;

        case SECOND_MENU_ID:
            Intent second = new Intent(this,TipCalculator.class);
            startActivity(second);
            finish();
        return true;

        }

        return super.onOptionsItemSelected(item);
    }
    public class TipCalculator extends Calculator{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tipcalculator);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_tipcalculator, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    Intent main = new Intent(this,Calculator.class);
    startActivity(main);
    finish();
    return super.onOptionsItemSelected(item);
}

}

菜单显示良好,主页按钮将刷新主页,但提示计算器按钮给我一个强制关闭错误,并使我崩溃。

您正在调用finish()在启动新意图后的当前活动上-这不是在销毁您希望返回的活动吗?您是否已将TipCalculator活动添加到清单中?强制关闭错误=需要Logcat