-4

我有这个:

int tries;

和这个:

tries = 2;

基本上,我希望在用户尝试次数用完时出现另一个活动。

这是从变量中减去的内容:

            @Override
        public void onClick(View v) {
            tries--;
            triess.setText(tries + "try(s)");
        }
    });

但是,这是我的代码:

        if (tries == 0) {
        //countDown.cancel();
        //finish();
        Intent openGame = new Intent("com.kyle.expertemoji.loserclass");
        startActivity(openGame);
    }
4

3 回答 3

3

onClick()如果您希望它在用户单击时运行,则将代码放入其中,Button因为它仅在Activity创建时运行,如果它在onCreate()

          @Override
    public void onClick(View v) // button is clicked
    {
        // TODO Auto-generated method stub
        tries--;  // tries is decrimented so if it was 1 it is now 0
        triess.setText(tries + "try(s)");

        if (tries == 0)   // this will be true from the comment above and will run the code starting the Activity
        {
            //countDown.cancel();
           //finish();
            Intent openGame = new Intent("com.kyle.expertemoji.loserclass");
            startActivity(openGame);
        }
    }
});

如果您希望该if语句在其他时间运行,请指明何时并显示该代码,我们可以提供更好的帮助。

此外,在此处发布时,请指出确切的问题,这样我们就不必猜测并且可以更轻松地帮助您。告诉我们你想要什么而不告诉我们发生了什么/没有发生什么是不够的。您可能会遇到错误、崩溃、编译错误、启动Activity太早等...请帮助我们帮助您

于 2013-06-06T20:12:22.037 回答
3

这是我的 WAG(Wild-assed-guess)答案,没有看到你的其余代码。

尝试将您的 if 语句移到onClick

所以:

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        tries--;
        triess.setText(tries + "try(s)");

        if (tries == 0) {
            //countDown.cancel();
            //finish();
            Intent openGame = new Intent("com.kyle.expertemoji.loserclass");
            startActivity(openGame);
        }
    }
于 2013-06-06T20:14:07.433 回答
0

Intents are case sensitive. Could it be that you are missing the case or spelling mistake. Check in your manifest for exact match.

You can also use

startActivity(new Intent(context, ClassName.class);
于 2013-06-06T20:35:45.263 回答