点击第一个按钮后,我无法点击另一个按钮

时间:2018-02-19 15:45:40

标签: java android android-studio

我是Java和android工作室的新手,我正在构建一个应用程序,当我在模拟器上测试它时,我可以点击一个按钮,它会调出我编程的内容,但是当我回到主界面时屏幕并尝试点击另一个按钮甚至我刚刚点击的同一个按钮,它不会做任何事情。我似乎无法找到我错过的或者我忘了添加一些东西。我也没有收到任何错误。

//On Click Listener for all info buttons
final View.OnClickListener mGlobal_OnClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.Areainfo:
                Intent area = new Intent(Layout.this, AreaInfo.class);
                startActivity(area);
                //Inform user Areainfo is selected
                Toast.makeText(Layout.this, "Area Info selected", Toast.LENGTH_SHORT).show();
                break;
            case R.id.Runinfo:
                Intent run = new Intent(Layout.this, RunInfo.class);
                startActivity(run);
                //Inform user Runinfo is selected
                Toast.makeText(Layout.this, "Run Info selected", Toast.LENGTH_SHORT).show();
                break;
            case R.id.Runnuminfo:
                Intent runnum = new Intent(Layout.this, RunNumInfo.class);
                startActivity(runnum);
                //Inform user Runnuminfo is selected
                Toast.makeText(Layout.this, "Number of Runs Info selected", Toast.LENGTH_SHORT).show();
                break;
            case R.id.Nmininfo:
                Intent Nmin = new Intent(Layout.this, NminInfo.class);
                startActivity(Nmin);
                //Inform user Nmininfo is selected
                Toast.makeText(Layout.this, "Nmin Info selected", Toast.LENGTH_SHORT).show();
                break;
            case R.id.Zvalinfo:
                Intent Zval = new Intent(Layout.this, ZvalInfo.class);
                startActivity(Zval);
                //Inform user Zvalinfo is selected
                Toast.makeText(Layout.this, "Zvals Info selected", Toast.LENGTH_SHORT).show();
                break;
            case R.id.Calculate:
                Intent Imperial = new Intent(Layout.this, ImperialCalc.class);
                startActivity(Imperial);
                Toast.makeText(Layout.this, "Calculating", Toast.LENGTH_SHORT).show();
                break;

2 个答案:

答案 0 :(得分:4)

尝试从每个setContentView

中删除case

答案 1 :(得分:0)

正如亚历山大建议你需要从每个案例中删除setContentView()。只在onCreate()中调用一次就足够了。出现问题的是setContentView()您的当前View被移除,包括您的所有onClickListener()。因此,返回Activity并按下按钮将无效,因为onClickListeners()已消失。

相关问题